blah blah blah is here! blah blah » Close

up2down
link

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.

last answered one month ago

3 answers

link

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.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private delegate void DoneHandler(); //This is the delegate in charge of handling events.
private event DoneHandler Done; //This is an event which will be handled by the delegate.

private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Waiting...";
textBox1.Refresh();
Done += new DoneHandler(SetTextDone); //Here we wire up an eventhandler for the "Done" event.
//do some work
DoWork();
}

private void DoWork()
{
System.Threading.Thread.Sleep(5000);
Done(); //This line calls the eventhandler we wired up earlier.
}

private void SetTextDone()
{
textBox1.Text = "SUCCESS";
}
}


You can find a better example of how to use delegates here

thank you Foamy

but the link you gave in http://www.megaupload.com/?d=EWIQBEAM does not able to download the DelegateDemo.zip

foamy
1194

You need to enter the "Captcha" letters to download it - on the right side of the download page.

up2down
link

You could reform your code like this:

private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Waiting...";
textBox1.Refresh();
System.Threading.Thread.Sleep(5000);
// ...code executing some events....
textBox1.Text = "SUCCESS";
}


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:

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";
}
}


Of course, if you're doing this in a single class, there's nothing stopping you from doing something like:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Waiting...";
textBox1.Refresh();
//do some work
DoWork();
}

private void DoWork()
{
System.Threading.Thread.Sleep(5000);
Done();
}

private void Done()
{
textBox1.Text = "SUCCESS";
}
}


Alternatively, you could have an instance variable in your form and use that ... something like:

public partial class Form1 : Form
{
private bool done = false;
private Timer timer;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "Waiting...";
textBox1.Refresh();
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
//do some work
DoWork();
}

private void DoWork()
{
System.Threading.Thread.Sleep(5000);
done = true;
}

private void timer_Tick(object sender, EventArgs e)
{
if (done)
{
done = false;
textBox1.Text = "SUCCESS";
}
}
}

gsvirdi
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.

up0down
link

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"; }}

Feedback