csstome 发表于 2013-1-27 13:01:01

Control.CheckForIllegalCrossThreadCalls=false不可在多线中随便使用

CodeProject上有篇文章An Alternate Way of Writing a Multithreaded GUI in C#
本意是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中移植代码的时候需要花费数倍的人工。
<div style="padding: 4px 5.4pt; width: 95%;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Collections.Generic;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.ComponentModel;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Data;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Drawing;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Text;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Windows.Forms;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifnamespaceWindowsApplication34
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicpartialclassForm1:Form
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicForm1()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifInitializeComponent();
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.gifvoidtest()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstringstrText=Guid.NewGuid().ToString();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.label1.Text=strText;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifif(this.label1.Text!=strText)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//测试性能时把这一句注释掉!!!!!!!!!!!!!!!!!!!!!!1
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthrownewException("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.gifvoiddoThread()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Diagnostics.Stopwatchsw=newSystem.Diagnostics.Stopwatch();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsw.Reset();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsw.Start();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffor(inti=0;i<100;i++)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giftest();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Threading.Thread.Sleep(10);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsw.Stop();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifConsole.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.gifpublicdelegatevoiddTest();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifvoidinvokeThread()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Diagnostics.Stopwatchsw=newSystem.Diagnostics.Stopwatch();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsw.Reset();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsw.Start();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffor(inti=0;i<100;i++)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifif(this.InvokeRequired)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthis.Invoke(newdTest(test));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifelse
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giftest();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifSystem.Threading.Thread.Sleep(10);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifsw.Stop();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifConsole.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.gifprivatevoidbutton1_Click(objectsender,EventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifForm1.CheckForIllegalCrossThreadCalls=false;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifnewSystem.Threading.Thread(newSystem.Threading.ThreadStart(doThread)).Start();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifnewSystem.Threading.Thread(newSystem.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.gifprivatevoidbutton2_Click(objectsender,EventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifnewSystem.Threading.Thread(newSystem.Threading.ThreadStart(invokeThread)).Start();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifnewSystem.Threading.Thread(newSystem.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}
页: [1]
查看完整版本: Control.CheckForIllegalCrossThreadCalls=false不可在多线中随便使用