C# how to change textbox value by loop? - c#

I need to create a script such as chronometer.
I write a code like following;
for(int i=0;i<50;i++)
{
textBox.Text = i.Tostring();
Task.Delay(100).Wait();
}
The expected output is like a chronometer ; an increasing text by 1 up to 49 started from 0 at textbox.
But I get only 49 after a 49*100 miliseconds pause later.
How can I solve this ?

The event or method running this piece of code needs to be asynchronous. This is in order for the UI to be responsive:
private async void btnDoWork_Click(object sender, EventArgs e)
{
for(int i=0;i<50;i++)
{
textBox.Text = i.Tostring();
await Task.Delay(100);
}
}
Otherwise, you'll be blocking the UI Thread and you will not be able to see the Text Box changing. You'll only see the last change which is 49 in your case.

Related

C# - Refresh WPF RichTextBox during a loop

I'm new learner in C# and i have a simple problem.
I do classic loop of 15 iterations with a Threat.Sleep of 3 secondes in each ones and inside this loop at each iteration i'm addind text to my RichTextBox.
BUT the text only appear at the end of the 15 iterations of 3 secondes ! :(
I want the text to be added at each iteration in the loop :) Is there a way to do that ?
Thank you for helping :)
AMIGA RULEZ
I tested this code and it worked.
When i press the button, the RTB adds the word "hello" each 3 seconds.
private async void button1_Click(object sender, EventArgs e)
{
for(int i=0; i<16; i++)
{
richTextBox1.Text += "hello";
await Task.Delay(3000);
}
}
Thread.Sleep will block the current thread. When a thread is blocked it cannot do anything else. So if it is the UI thread it cannot re-render the UI until the thread is unblocked. Because of this it is recommended to never block the UI thread.
A workaround is to use Task.Delay and async/await instead. Internally this will cause your code to be rewritten to a state machine. In principle transforming it to something like the following pseudo code
int i = 0;
timer = new Timer(TimerCallback, period: 3s);
timer.Start();
...
public void TimerCallback(){
if(i >= 16){
timer.Stop();
return;
}
richTextBox1.Text += "hello";
}

C# WPF click event

I wonder why click event does not show me the hp value of every loop. Its only shows me hp at the start and 0 at the end
private void button_Click(object sender, RoutedEventArgs e)
{
while (player.Hp > 0)
{
int vypocet = player.Damage(player2);
player.Hp = vypocet;
label.Content = vypocet;
}
}
This should be everything you need to know
So as i said its only show me start hp and hp after whole fight and i dont know why its not show me other numbers if i am using while loop
The reason is that the event handler runs on the UI thread. This means, the changed value can be reflected in the user interface only after the whole loop ends.
If you wanted to show the progress, you would have to run the computation on another thread and use the Dispatcher to notify the UI thread about the changes.
An alternative is to yield the UI thread regularly to give the UI a chance to update. This is however not very clean.
await Dispatcher.Yield(DispatcherPriority.ApplicationIdle);
Because UI controls will be updated after button_Click method exits.
Try change method to asynchronous and use Task.Delay which will "release" UI thread for updating controls
private async void button_Click(object sender, RoutedEventArgs e)
{
while (player.Hp > 0)
{
int vypocet = player.Damage(player2);
player.Hp = vypocet;
label.Content = vypocet;
await Task.Delay(100);
}
}

Update GUI components from Begininvoke

So I have a very basic Windows Application where I want to count from 1 to 10000
and show the numbers in label:
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(() =>
{
for (int i = 0; i < 10000; i++)
{
BeginInvoke((MethodInvoker)delegate ()
{
label3.Text = i.ToString();
});
}
});
thread.Start();
}
The problem is that the label text doesn't update and shows only the last loop counter i.e. 9999. Is BeginInvoke called on UI thread? Why does not the label get updated correctly?
Thanks.
Because BeginInvoke is an asynchronous call, you're sending too many updates to the text box for it to update fast enough, by the time the text box has got around to drawing, it's already counted up to 10000!
You can synchronously update the text, that is, the calling loop will halt until the text box has updated and finished, use Invoke instead of BeginInvoke.

Update GUI in a loop [duplicate]

This question already has answers here:
Can I use a timer to update a label every x milliseconds
(4 answers)
Closed 7 years ago.
I want to print "ms" value in textbox1 by clicking the button firstly, then it should wait for 100ms (one by one), then it show next value.
But it's showing only the last value by clicking button, still after coding Thread.Sleep(100). It should show the value like stop watch.
private void button1_Click(object sender, EventArgs e)
{
int ms = 00;
for (ms = 0; ms < 10; ms++)
{
textBox1.Text = ms.ToString();
Thread.Sleep(100);
textBox1.Text = "";
}
}
The reason your textbox only shows tha last value is because your code runs on the UI thread. The UI doesn't get the time to update its layout until your code has reached its end.
You could solve this by using some sort of background threading mechanism so that the UI has the time and resources to update itself.
You could use timers and update the textbox text on its tick event.
You could use task await to enforce background threading instead of Thread.Sleep
private async void button1_Click(Object sender, EventArgs e)
{
for (int ms = 0; ms < 10; ms++)
{
textBox1.Text = ms.ToString();
await Task.Delay(100);
}
}
Thats because you button click is running on the gui thread and will block painting.
You could try updating the textbox:
private void button1_Click(object sender, EventArgs e)
{
int ms = 00;
for (ms = 0; ms < 10; ms++)
{
textBox1.Text = ms.ToString();
textBox1.Update();
Thread.Sleep(100);
textBox1.Text = "";
}
}
But this will still block you gui thread. If you don't want to block the current thread, you could create a new Thread that invokes the gui thread to alter the textBox1.Text.
Writing to a TextBox from another thread?

asking data from external device with selected delay using C#

private void timer1_Tick(object sender, EventArgs e)
{
string[] DF_Engines = { Form2.Engine1, Form2.Engine2, Form2.Engine3, Form2.Engine4, Form2.Engine5, Form2.Engine6 };
foreach (string DF_Engine in DF_Engines)
{
if (Convert.ToDouble(DF_Engine) != 99)
{
string Hex_ADD1 = "{" + DF_Engine + "|";
Console.WriteLine(Hex_ADD1);
serialPort1.Write(Hex_ADD1);
n = Convert.ToInt16(DF_Engine);
}
}
}
Form2.Engine1, Form2.Engine2....... are the values come from Form2 when the corresponding checkbox is selected. these would be 1, 2 , 3 so on....
My code send 01 when checkbox1 is selected but it send 01,02 without any delay when checkbox1 and checkbox2 are selected in Form2.
I need delay when asking 01 and 02 and so on as per my interest.
how could i do it convineintly
and when i use Thread.Sleep(500), the application gets slow.
need guidance.
Do not use Thread.Sleep on UI thread. It will definitely slow down your application. Rather start a new thread, pass data to that thread and let that thread send input to your application with delay.
System.Threading.Thread thread = new System.Threading.Thread((inputList) =>
{
foreach (var input in inputList as IEnumerable<int>)
{
//Send input
System.Threading.Thread.Sleep(500);
}
});
thread.Start();
Where inputList is an array of data(1,2,etc depending upon your checkbox selected).
You can consider using a separate thread of just a BackgroundWorker control. This way, you could use Thread.Sleep() method and it should not make the application's GUI unresponsive. You can find some more information about it on msdn: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx.
Just add a BackgroundWorker control to your form and use this code to handle appropriate events:
private void timer1_Tick(object sender, EventArgs e)
{
string[] DF_Engines = { Form2.Engine1, Form2.Engine2, Form2.Engine3, Form2.Engine4, Form2.Engine5, Form2.Engine6 };
//disable timer1, so it wont tick again until the current work is finished
timer1.Stop();
//start processing asynchronously, so GUI is still responsive
sampleBackgroundWorker.RunWorkerAsync(DF_Engines);
}
//this method should be attached to DoWork event of the BackgroundWorker
private void sampleBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
string[] DF_Engines = (e.Argument as string[]);
foreach (string DF_Engine in DF_Engines)
{
if (Convert.ToDouble(DF_Engine) != 99)
{
string Hex_ADD1 = "{" + DF_Engine + "|";
Console.WriteLine(Hex_ADD1);
serialPort1.Write(Hex_ADD1);
//n gets overwritten in each iteration, is this line required?
n = Convert.ToInt16(DF_Engine);
Thread.Sleep(500);
}
}
//you can also pass a result from this method back to the GUI thread like this
//e.Result = "job done";
//this can be read later in RunWorkerCompleted method of the BackgroundWorker
}
//this method should be attached to RunWorkerCompleted event of the BackgroundWorker
private void sampleBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//start timer1, so it can invoke the worker again
timer1.Start();
}
If you need more help with this, let me know.

Categories