SAKryukov for sure meant to make the thread sleep for one second at a time and use a counter to remember how many 1-second-sleep-cycles have passed.
You could try something like this:
private void CallToReConnect()
{
for(int counter = 20; counter >= 0; counter--)
{
System.Threading.Thread.Sleep(1000);
ShowProgress(counter);
}
}
private delegate void ShowProgressDelegate(int progress);
private void ShowProgress(int secondsToWait)
{
if(statusLabel1 == null)
return;
if(statusLabel1.InvokeRequired)
{
statusLabel1.Invoke(
new ShowProgressDelegate(ShowProgress),
new object[]{secondsToWait}
);
return;
}
statusLabel1.Text= "ReConnect in " + secondsToWait.ToString() + " secs";
}
This should make the invoking work.
As for causing the application to crash by shutting it down, that's what you wanted. The application is shut down.
No. I'm just kidding.
You could set your
BackgroundWorker
to
SupportsCancellation = true
and call
CancelAsync()
on it when shutting the application down.
This way, you could notice the
CancellationPending
property to become
true
. Check it from within the loop and stop
CallToReconnect
when appropriate.
Surely someone knows of a nicer way of invoking with less code and without the need for a seperate method
and delegate?