-2

I have a while loop inside a delegate method that is called. My issue is that while it is inside the while loop the whole GUI is frozen until it exits the while loop. How can I make it so it does not freeze the GUI? Thanks

if (!IsUploadingAvailable())
{
    MessageBox.Show("Uploading is not available, please wait until it is ready!", "Upload not available");
    myButton.Enabled = false;

    while (IsUploadingAvailable())
    {
        Thread.Sleep(RandomAmountOfTime(10000)); 
    }
    MessageBox.Show("Uploading is now available!");
}
Abhineet Verma
  • 1,008
  • 8
  • 18
user3774466
  • 23
  • 1
  • 4

5 Answers5

0

You are blocking the GUI thread. You need to do your work on a different thread, e.g. by using the BackgroundWorker class.

Manuel Barbe
  • 2,104
  • 16
  • 21
0

You should use Timer

Try This:

System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=10000;//10 seconds
timer1.Tick += new System.EventHandler(timer1_Tick);

if (!IsUploadingAvailable())
{
    MessageBox.Show("Uploading is not available, please wait until it is ready!", "Upload not available");
    myButton.Enabled = false;
    while (IsUploadingAvailable())
    {
         timer1.Start();
    }
    MessageBox.Show("Uploading is now available!");
}

private void timer1_Tick(object sender, EventArgs e)
{
     //do whatever you want 
      timer1.Stop();
}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
0

As answered here :- Thread sleep in a for loop

You are blocking the UI thread - no updates will usually show until you leave the event-handler. A hacky approach is to use Application.DoEvents(), but this is lazy and risks re-entrancy especially if you are pausing.

A better approach is to do the work on a background thread, and use Invoke to push the data to the UI (don't talk to the UI from the worker thread).

Community
  • 1
  • 1
Neel
  • 11,625
  • 3
  • 43
  • 61
0

One of the most important things that differentiates a “quick and dirty” application from one that has been designed well is how the application’s user interface behaves during lengthy operations. The quick-and-dirty approach is to just do all of your work in a button’s Click event handler and not worry about the user interface. The problem with this is that the GUI will freeze up while the application does whatever work it needs to do.

A well designed application, on the other hand, is one that is careful to do as much work as possible in background threads, keeping the GUI responsive and making sure that it makes it obvious to the user that work is going on in the background and adjusts the GUI to disallow any user actions that don’t apply until after the work finishes.

BackgroundWorker was designed for exactly this kind of scenario.

See MSDN Or use the Task Parallel Library

Ricky
  • 2,323
  • 6
  • 22
  • 22
0

Use Task:

if (!IsUploadingAvailable())
{
    MessageBox.Show("Uploading is not available, please wait until it is ready!", "Upload not available");
    myButton.Enabled = false;

    await WaitForUploadingAvailable();

    MessageBox.Show("Uploading is now available!");
}

async Task WaitForUploadingAvailable()
{
    await Task.Run(() =>
    {
        while (!IsUploadingAvailable())
        {
            Thread.Sleep(RandomAmountOfTime(10000));
        }
    });
}

Declare the method that contains your code as async.

Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32