执行长时间运行的UI更改而不会阻塞主线程 [英] Performing long running UI changes without blocking the main thread

查看:270
本文介绍了执行长时间运行的UI更改而不会阻塞主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何,在不影响C#winforms应用程序中的主线程的情况下,是否可以运行大量UI更新?

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)

我知道我可以在事件中使用BackgroundWorker执行冗长的操作,但是问题是您无法在此事件中更改任何UI(这将导致跨线程异常)-所以我不能放我的大量关闭表单都在这里打电话.

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.

推荐答案

您应使用ProgressChanged 事件" rel ="nofollow"> BackgroundWorker 更新用户界面.要启用此功能,请设置您的 WorkerReportsProgress 属性 BackgroundWorker 实例添加到true.然后,您可以通过从 DoWork 事件处理程序:

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);

建议使线程安全调用Windows窗体控件.原因如下:

访问Windows Forms控件本质上不是线程安全的.如果你 有两个或多个线程来控制控件的状态,它是 可能会迫使控件进入不一致状态.其他 与线程相关的错误也是可能的,包括竞争条件 和僵局.确保访问控件很重要 以线程安全的方式完成.

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.

.NET Framework可帮助您检测何时访问自己的 以非线程安全的方式进行控制.当你跑步时 您在调试器中的应用程序,以及一个线程之外的线程 它创建了一个控件,试图调用该控件,调试器 引发InvalidOperationException消息,控制控件 从不是在其上创建线程的线程访问的名称."

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.

您可以禁用该异常:

Form.CheckForIllegalCrossThreadCalls = false;

但是控件可以(有时会)停止工作.

But controls could (and sometime will) stop working.

这篇关于执行长时间运行的UI更改而不会阻塞主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆