this may seem trivial, but I still have not figured out how to do it.
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Waiting...";
System.Threading.Thread.Sleep(5000);
// ...code executing some events....
textBox1.Text = "SUCCESS";
}
with a click of a button, I want to show the text 'Waiting ...'
and when some code excuting some events has finsihed, I want to show the text 'SUCCESS', in the same textbox.
but the above code always only show 'SUCCESS', not matter how long the duration of sleep I set.
I believe this can be done, there could be something which I miss out in the settings...
Thanks.

3 answers
The best way to learn about it, is to step through the code while its running. Below are some - hopefully helpful - comments on the code.
You can find a better example of how to use delegates here
answered one month ago by:
1194
260
thank you Foamy
260
but the link you gave in http://www.megaupload.com/?d=EWIQBEAM does not able to download the DelegateDemo.zip
1194
You need to enter the "Captcha" letters to download it - on the right side of the download page.
You could reform your code like this:
but really, you should have your eventhandler(s) fire off an event when they're done telling you that they've finished. You can then react to this event and set the text accordingly ... something like:
Of course, if you're doing this in a single class, there's nothing stopping you from doing something like:
Alternatively, you could have an instance variable in your form and use that ... something like:
answered one month ago by:
1194
71
I would suggest that u should disable the button after the user click; and u should enable the button when the success is displayed. This will make sure that the user should not be pressing the button again to confuse the code + it will give a nice look to the waiting process too.
hi Foamy,
I am trying to learn the concepts of Delegates and Events, but always find it confusing....your code would be a good example... could you explain in details of your code below on Delegates and Events?
public partial class Form1 : Form{ public Form1() { InitializeComponent(); } private delegate void DoneHandler(); private event DoneHandler Done; private void button1_Click(object sender, EventArgs e) { textBox1.Text = "Waiting..."; textBox1.Refresh(); Done += new DoneHandler(SetTextDone); //do some work DoWork(); } private void DoWork() { System.Threading.Thread.Sleep(5000); Done(); } private void SetTextDone() { textBox1.Text = "SUCCESS"; }}
answered one month ago by:
260