CheckForIllegalCrossThreadCalls=false 的后果 [英] Ramifications of CheckForIllegalCrossThreadCalls=false

查看:44
本文介绍了CheckForIllegalCrossThreadCalls=false 的后果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将一个应用程序从 VS2003 更新到了 VS2008,我知道我将处理大量跨线程操作无效:从创建它的线程以外的线程访问的控制‘myControl’"我是以我认为正确的方式处理此问题(请参阅下面的代码示例).我遇到了许多需要类似修复的控件.不希望非 UI 线程访问的每个标签、文本框等都有类似的代码.为整个应用程序设置 CheckForIllegalCrossThreadCalls = false 有什么后果?

I recently updated an application from VS2003 to VS2008 and I knew I would be dealing with a host of "Cross-thread operation not valid: Control 'myControl' accessed from a thread other than the thread it was created on" I am handling this in what I beleive is the correct way (see code sample below). I am running into numerous controls that are going to need a similar fix. Not wanting to have similar code for every label, textbox etc.. that are being accessed by a non UI thread. What are the ramifications of just setting the CheckForIllegalCrossThreadCalls = false for the entire app?

我发现了一篇 CodeProject 文章,其中包含各种解决方法和底部警告不要设置属性.我正在寻找有关此问题的其他意见/经验.

I found a CodeProject article with various workarounds and a warning at the bottom to NOT set the property. I am looking for other opinions/experiences on this issue.

private void ShowStatus(string szStatus)
{
    try
    {
        if (this.statusBar1.InvokeRequired) { BeginInvoke(new MethodInvoker(delegate() { ShowStatus(szStatus); })); }
        else { statusBar1.Panels[0].Text = szStatus; }
    }
  catch (Exception ex)
  {
    LogStatus.WriteErrorLog(ex, "Error", "frmMNI.ShowStatus()");
  }
}

我找到了另一篇文章,其中提供了一些可能的解决方案SO Question 2367718

I found another article with some possible solutions SO Question 2367718

推荐答案

当你不调试时,你仍然会遇到问题.

When you're not debugging, you'll still have problems.

来自 Control.CheckForIllegalCrossThreadCalls 的文档:

请注意,当应用程序在调试器之外启动时,非法的跨线程调用总是会引发异常.

Note that illegal cross-thread calls will always raise an exception when an application is started outside the debugger.

您需要更正问题.

话虽如此,你提到:

不希望非 UI 线程访问的每个标签、文本框等都使用类似的代码.

Not wanting to have similar code for every label, textbox etc.. that are being accessed by a non UI thread.

我会重新考虑这个立场.您应该尝试将在单独线程上运行的逻辑移动到单独的方法或类中,这反过来会使将调用编组回 UI 变得更加简单.随着时间的推移,这将使您的代码更加可靠和易于维护.

I would reconsider this stance. You should try to move the logic running on a separate thread into separate methods or classes, which will in turn make marshaling the calls back into the UI much simpler. Over time, this will make your code much more reliable and maintainable.

请注意,您也可以使用 Control.Invoke 在一次调用中将一整套对 UI 的调用编组,而不是单独执行每个设置操作.当你完成时,他们真的不应该有那么多.

Note that you can use Control.Invoke to marshal a whole set of calls to the UI in one call, too, instead of doing each single set operation individually. There really shouldn't be that many of them, when you finish.

例如,听起来您正在加载数据.假设你有(在你的后台线程上),你的数据加载方法:

For example, it sounds like you're loading the data. Say you have (on your background thread), your data loading method:

var myData = LoadData();
this.Invoke( new Action( () =>
    {
        // Just set all of your data in one shot here...
        this.textBox1.Text = myData.FirstName;
        this.textBox2.Text = myData.LastName;
        this.textBox3.Text = myData.NumberOfSales.ToString();
    }));

这篇关于CheckForIllegalCrossThreadCalls=false 的后果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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