Milos
Milos

Reputation: 1465

Proper way to display wait dialog

I have WaitDialog with Marquee style ProgressBar.

  1. Is this a proper way to display it ?

        var wd = new WaitDialog();
        Task.Factory.StartNew(() =>
        {
            LongRunningMethod();
            wd.Close();
        });
        wd.ShowDialog();
    
  2. Please recommend a proper way to report progress from a Task for non Marquee ProgressBar.

Upvotes: 1

Views: 1818

Answers (1)

Brad Rem
Brad Rem

Reputation: 6026

What I think you're looking for is a way to run your long running method and display a progress dialog without locking up the UI. You also need to watch out for cross-threading issues by accessing your UI from a different thread.

What I would suggest is to pattern it this way which will keep your application responsive while the task is running:

    var wd = new WaitDialog();
    wd.Show(); // Show() instead of ShowDialog() to avoid blocking
    var task = Task.Factory.StartNew(() => LongRunningMethod());
    // use .ContinueWith to avoid blocking
    task.ContinueWith(result => wd.Invoke((Action)(() => wd.Close())));

You show your progress dialog -- whether it has a marquee or not is of no consequence -- and then you spin up your LongRunningMethod on its own task. Use the .ContinueWith method to close your dialog when the task has completed and to also avoid blocking the rest of your program.

Upvotes: 5

Related Questions