---
title: "Control.CheckForIllegalCrossThreadCalls=false不可在多线中随便使用"
author: "csstome"
date: "2007-12-10T14:16:00Z"
tags: []
slug: "control-checkforillegalcrossthreadcalls-false"
draft: false
lastmod: "2007-12-10T14:16:00Z"
id: 1033166
url: "https://csstome.iteye.com/blog/1033166"
---

CodeProject上有篇文章[An Alternate Way of Writing a Multithreaded GUI in C#](http://www.codeproject.com/KB/user-controls/AlternateMultithreadedGUI.aspx)

本意是Alternate Way 另外一种方法,后来莫名其妙的被转载成中文变了题目，最高效的方法。

CheckForIllegalCrossThreadCalls和control.Invoke有什么不同，哪个更好用，更高效呢？  
*占在任何角度讲，都不要使用CheckForIllegalCrossThreadCalls，即便他运行和代码编写的确实比Invoke效率高* 。  
感兴趣的，可以参考我后面贴出的代码来测试对比一下两者的不同。这里我只简单说一下结论：  
1、性能CheckForIllegalCrossThreadCalls=false时比invoke高，而且代码比较优雅。  
测试时间如下：  
Button1 ---------------------------  
00:00:01.0760900  
00:00:01.0771200  
Button2 --------------------------  
00:00:01.0812499  
00:00:01.0813443  
效率差多少？在这里时间还不到1%，代码少写一个if字句  
看到有文章说这种方法在大量更新ui时会引发大量异常，导致性能下降  
我测试了一下，耗时和循环次数是很平稳的线性关系，而且也没有发现几个Exception相关性能计数器有问题，这说明这又是某老外朋友想当然的说法。

2、CheckForIllegalCrossThreadCalls在.net1.x中默认是false，也就是不检查，.net2.0和3.x默认是true  
说明这是ms有意的引导，说不定以后不让你改了。这也是很多1.x用户在刚用2.0时不习惯跨线程更新ui的原因之一。

3、死穴：安全性  
CheckForIllegalCrossThreadCalls容许子线呈随时更新ui，在同一个test函数体内，不能保证自身事务的一致性。给label1付了值  
一回头，就已经被别人改了，这和超市的踩踏事件的后果一样严重。  
当然你可以自己加锁，用信号量，这样还不如直接使用Invoke了，你只是又把别人做好的事情做了一遍。

如果你觉的你的应用不会考虑在写入ui的同时来读取ui，而倾向使用CheckForIllegalCrossThreadCalls来追求效率的话，也是不恰当的做法。  
首先CheckForIllegalCrossThreadCalls并不能让效率发生本质的变化。  
其次需求永远是变化的，现在不考虑不等于以后不会碰到  
听从ms的引导。否则以后要在高版本的.net framework中移植代码的时候需要花费数倍的人工。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)using System;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) using System.Collections.Generic;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) using System.ComponentModel;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) using System.Data;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) using System.Drawing;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) using System.Text;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) using System.Windows.Forms;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) namespace WindowsApplication34  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public partial class Form1:Form  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public Form1()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) InitializeComponent();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 模拟一个实际应用  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 对label1付值后立马检查他的值,如果已经被改过则抛出异常  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) void test()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) string strText = Guid.NewGuid().ToString();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) this .label1.Text = strText;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) if ( this .label1.Text != strText)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 测试性能时把这一句注释掉！！！！！！！！！！！！！！！！！！！！！！1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) throw new Exception( " label1的值意外改变 " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 不使用invoke方法直接进入Control  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) void doThread()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.Diagnostics.Stopwatchsw = new System.Diagnostics.Stopwatch();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) sw.Reset();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) sw.Start();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) for ( int i = 0 ;i &lt; 100 ;i ++ )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) test();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.Threading.Thread.Sleep( 10 );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) sw.Stop();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine(sw.Elapsed);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 使用invoke方法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) public delegate void dTest();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) void invokeThread()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.Diagnostics.Stopwatchsw = new System.Diagnostics.Stopwatch();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) sw.Reset();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) sw.Start();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) for ( int i = 0 ;i &lt; 100 ;i ++ )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) if ( this .InvokeRequired)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) this .Invoke( new dTest(test));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) test();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) System.Threading.Thread.Sleep( 10 );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) sw.Stop();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Console.WriteLine(sw.Elapsed);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 通过CheckForIllegalCrossThreadCalls的值忽略跨线程错误,这时会抛出异常  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private void button1\_Click( object sender,EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) Form1.CheckForIllegalCrossThreadCalls = false ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) new System.Threading.Thread( new System.Threading.ThreadStart(doThread)).Start();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) new System.Threading.Thread( new System.Threading.ThreadStart(doThread)).Start();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) // 通过invoke方法,在主线程排队,lable1的值在test函数体内永远是线程安全的.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) private void button2\_Click( object sender,EventArgse)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif) ... {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) new System.Threading.Thread( new System.Threading.ThreadStart(invokeThread)).Start();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) new System.Threading.Thread( new System.Threading.ThreadStart(invokeThread)).Start();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }