Unfreeze the windows GUI

This example shows how to react on user events if the windows GUI is blocked by a longer process.

// You need the Threading library to use the 
// "Thread.Sleep" function
// using System.Threading;
// This boolean value can be used to stop the
// process below
bool UserHitCancel = false;
this.progressBar.Maximum = 100;
// Loop 100 times
for (int i = 0; (i < 100 && !UserHitCancel); i++)
    // Update a progressbar or any other control
    this.progressBar.Value = i;
    // This function checks if the user fired any
    // events, like clicks on a cancel button and 
    // allowes the application to react 
    // This tells the current thread to sleep for 50 milliseconds,
    // so that the user can see the progress bar filling up
Snippet Details

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!

Older comments:

DuMaSexy March 11, 2011 at 13:35
Use windows form timer..