I want to display a progress bar while doing some work, but that would hang the UI and the progress bar won’t update.
I have a WinForm ProgressForm with a ProgressBar that will continue indefinitely in a marquee fashion.
using(ProgressForm p = new ProgressForm(this))
{
//Do Some Work
}
Now there are many ways to solve the issue, like using BeginInvoke, wait for the task to complete and call EndInvoke. Or using the BackgroundWorker or Threads.
I am having some issues with the EndInvoke, though that’s not the question. The question is which is the best and the simplest way you use to handle such situations, where you have to show the user that the program is working and not unresponsive, and how do you handle that with simplest code possible that is efficient and won’t leak, and can update the GUI.
Like BackgroundWorker needs to have multiple functions, declare member variables, etc. Also you need to then hold a reference to the ProgressBar Form and dispose of it.
Edit: BackgroundWorker is not the answer because it may be that I don’t get the progress notification, which means there would be no call to ProgressChanged as the DoWork is a single call to an external function, but I need to keep call the Application.DoEvents(); for the progress bar to keep rotating.
The bounty is for the best code solution for this problem. I just need to call Application.DoEvents() so that the Marque progress bar will work, while the worker function works in the Main thread, and it doesn’t return any progress notification. I never needed .NET magic code to report progress automatically, I just needed a better solution than :
Action<String, String> exec = DoSomethingLongAndNotReturnAnyNotification;
IAsyncResult result = exec.BeginInvoke(path, parameters, null, null);
while (!result.IsCompleted)
{
Application.DoEvents();
}
exec.EndInvoke(result);
that keeps the progress bar alive (means not freezing but refreshes the marque)
It seems to me that you are operating on at least one false assumption.
1. You don’t need to raise the ProgressChanged event to have a responsive UI
In your question you say this:
Actually, it does not matter whether you call the
ProgressChangedevent or not. The whole purpose of that event is to temporarily transfer control back to the GUI thread to make an update that somehow reflects the progress of the work being done by theBackgroundWorker. If you are simply displaying a marquee progress bar, it would actually be pointless to raise theProgressChangedevent at all. The progress bar will continue rotating as long as it is displayed because theBackgroundWorkeris doing its work on a separate thread from the GUI.(On a side note,
DoWorkis an event, which means that it is not just “a single call to an external function”; you can add as many handlers as you like; and each of those handlers can contain as many function calls as it likes.)2. You don’t need to call Application.DoEvents to have a responsive UI
To me it sounds like you believe that the only way for the GUI to update is by calling
Application.DoEvents:This is not true in a multithreaded scenario; if you use a
BackgroundWorker, the GUI will continue to be responsive (on its own thread) while theBackgroundWorkerdoes whatever has been attached to itsDoWorkevent. Below is a simple example of how this might work for you.3. You can’t run two methods at the same time on the same thread
You say this:
What you’re asking for is simply not real. The “main” thread for a Windows Forms application is the GUI thread, which, if it’s busy with your long-running method, is not providing visual updates. If you believe otherwise, I suspect you misunderstand what
BeginInvokedoes: it launches a delegate on a separate thread. In fact, the example code you have included in your question to callApplication.DoEventsbetweenexec.BeginInvokeandexec.EndInvokeis redundant; you are actually callingApplication.DoEventsrepeatedly from the GUI thread, which would be updating anyway. (If you found otherwise, I suspect it’s because you calledexec.EndInvokeright away, which blocked the current thread until the method finished.)So yes, the answer you’re looking for is to use a
BackgroundWorker.You could use
BeginInvoke, but instead of callingEndInvokefrom the GUI thread (which will block it if the method isn’t finished), pass anAsyncCallbackparameter to yourBeginInvokecall (instead of just passingnull), and close the progress form in your callback. Be aware, however, that if you do that, you’re going to have to invoke the method that closes the progress form from the GUI thread, since otherwise you’ll be trying to close a form, which is a GUI function, from a non-GUI thread. But really, all the pitfalls of usingBeginInvoke/EndInvokehave already been dealt with for you with theBackgroundWorkerclass, even if you think it’s “.NET magic code” (to me, it’s just an intuitive and useful tool).