简体   繁体   中英

Performing long running UI changes without blocking the main thread

Is there anyway to run a large number of UI updates without effecting the main thread in a C# winforms application?

I would like to avoid a long delay when a user clicks a specific event (which in my case is many close form calls to dispose of memory)

I know i can use BackgroundWorker to perform lengthy operations in the "do work" event, but the problem is that you cant change any UI in this event (it will cause a cross thread exception) - so i cant put my large number of close form calls here.

And I cant put the close form calls in the "worker completed" event because this is run on the main thread, and will eventually lockup the main thread causing a bad user experience.

I have thought about spawning a thread to handle closes only when the appication session is idle, but not sure if this is going to be a bit messy.

You should use ProgressChanged event of BackgroundWorker to update UI. To enable this feature set WorkerReportsProgress property of your BackgroundWorker instance to true . Then you can update UI many times by sending data from DoWork event handler:

backgroundWorker.ReportProgress(percentage, yourCustomData);

It is recommended to Make Thread-Safe Calls to Windows Forms Controls . Here is the reason:

Access to Windows Forms controls is not inherently thread safe. If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state. Other thread-related bugs are possible as well, including race conditions and deadlocks. It is important to ensure that access to your controls is done in a thread-safe way.

The .NET Framework helps you detect when you are accessing your controls in a manner that is not thread safe. When you are running your application in the debugger, and a thread other than the one which created a control attempts to call that control, the debugger raises an InvalidOperationException with the message, "Control control name accessed from a thread other than the thread it was created on."

This exception occurs reliably during debugging and, under some circumstances, at run time. You are strongly advised to fix this problem when you see it.

You can disable that exception:

Form.CheckForIllegalCrossThreadCalls = false;

But controls could (and sometime will) stop working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM