c# - How to open a window with progress bar midway during task - Stack Overflow

admin2025-04-21  2

I find OriginalGriff‘s alternative progress reporting method quite interesting. But I ran into a problem, which I suspect I also will run into if I use the BackgroundWorker.

My idea is to start a long task and ‘report’ progress from the start. But I only want to bring up a dialog window with the progress bar after a second or two. I designed a dialog class implementing the IProgress interface for this. Thus I will not have any progress bar window if the task didn’t take much time after all.

But with my task and the dialog running in different threads, I wasn’t allowed to show the dialog midway through my lengthy task.

Is there a solution to this? (Note: I’m a C# newbie, but experienced in other programming languages).

I find OriginalGriff‘s alternative progress reporting method quite interesting. But I ran into a problem, which I suspect I also will run into if I use the BackgroundWorker.

My idea is to start a long task and ‘report’ progress from the start. But I only want to bring up a dialog window with the progress bar after a second or two. I designed a dialog class implementing the IProgress interface for this. Thus I will not have any progress bar window if the task didn’t take much time after all.

But with my task and the dialog running in different threads, I wasn’t allowed to show the dialog midway through my lengthy task.

Is there a solution to this? (Note: I’m a C# newbie, but experienced in other programming languages).

Share Improve this question asked Jan 22 at 22:40 user29809user29809 931 silver badge8 bronze badges 4
  • 1 This question is similar to: Updating GUI (WPF) using a different thread. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Ahmed AEK Commented Jan 22 at 22:54
  • Please see the previous comment. @Ahmed AEK is right, the question could be considered a duplicate. I only want to add the a dialog (if you mean ShowModal) is generally bad thing (excluding several tradition cases such as file dialogs or About dialog). Doing anything with a dialog from a non-UI threads is even a lot worse. – Sergey A Kryukov Commented Jan 23 at 0:54
  • Please be careful: Code Project recently went out of business, and the site provides only read-only access and exposes only the articles. A lot of other data is unreachable. The access is not available 100% of the time. If you see something valuable, save it. I created a mirror for all my articles. – Sergey A Kryukov Commented Jan 23 at 1:02
  • You create a "window" ("hidden") before you start the "background worker". The BGW then communicates with the window (Show; updates progress bar) via the ProgressReporting event (which runs on the UI thread). The BGW "knows" how long it has been running and can "time the reporting" accordingly. There's no "dialogs" because they're modal. – – Gerry Schmitz Commented Jan 23 at 9:23
Add a comment  | 

1 Answer 1

Reset to default 1

WinForms and WPF have a rule that only the primary thread (the UI thread) is allowed to touch visual elements such as windows and progress bars. Since the job is done on a secondary thread, that thread may not update UI elements. Not directly, at least.

If you want to update a visual element from a worker thread, you use Invoke (WinForms) or Dispatcher.Invoke (WPF).

WinForms:

this.Invoke (() => {
    progressForm.Show ();
    progressBar.Value++;
});

WPF:

this.Dispatcher.Invoke (() => {
    progressWindow.Show ();
    progressBar.Value++;
});
转载请注明原文地址:http://anycun.com/QandA/1745225653a90458.html