问题描述
我正在尝试找到一种方法,以使用户控件在其自己的UI线程中运行.这可能吗?我试图防止基于模块的应用程序由于单个模块而崩溃.
I'm trying to figure out a way to make user controls run in their own UI threads. Is this possible? I'm trying to prevent a module-based application from crashing due to a single module.
有什么想法吗?
推荐答案
不幸的是,所有UI控件都在同一UI线程上运行.因此,在此线程上运行的任何可能导致挂起情况的代码都需要使用某种超时逻辑进行编码.
Unfortunately all UI controls run on the same UI thread. Therefore any code running on this thread that could potentially lead to a hang situation would need to be coded with some sort of timeout logic.
DateTime startTime = DateTime.Now;
while(DateTime.Now.Subtract(startTime).TotalSeconds < 30)
{
//do something
}
否则,如Orlangur前面所述,所有事件处理程序代码都需要在单独的线程中运行.但是,您仍然需要监视这些线程,以确定它们是否运行时间过长并关闭它们.这样一来,您最好实现上面的逻辑类型,因为它将减少工作量,并提高可维护性.
Otherwise, as Orlangur stated earlier, all event handler code would need to be run in separate threads. You would still however need to monitor these threads to determine if they've been running too long and shut them down. As such you might as well implement the type of logic above as it would be a lot less work and more maintainable.
这篇关于在Windows窗体中获取多个UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!