日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

非UI线程操作UI

發布時間:2024/4/17 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 非UI线程操作UI 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
private void button1_Click(object sender, EventArgs e){new Thread(delegate(){this.label1.Text = "sdfas";}).Start();}

如果你開發過Windows Form項目,并使用過多線程操作UI,那么我相信你應該熟悉上面這段代碼,在非UI線程操作UI。

當你用F5 debug模式運行此項目,并點擊button1運行此段代碼時,你將會看到這個異常信息:

?

但是在我們使用Ctrl+F5或者直接到項目輸出目錄執行程序,并點擊button1的時候,label1的內容被修改成功了,并且沒有任何異常被拋出。

這是為什么呢?

一位英文名叫Xiao Liang 來自 MSRA 的工程師告訴我 most Windows native APIs / messages are thread-safe.(在非UI線程操作UI會有潛在的問題,當兩個線程在同一時間索取這個控件的話,問題就會油然而生了,呵呵)我們可以在非UI線程操作這個label1. 他還發現, “Control.CheckForIllegalCrossThreadCalls? is True?? with debugger,???? False? without debugger”. OK,主要的原因找到了,關鍵就是這個Control.CheckForIllegalCrossThreadCalls property.

當有詢問Xiao Liang我是否可以把他的這個研究結果公布在我的blog文章里時,他發給我了一片MSDN文章,說這篇文章說得比較清晰:

我讀過這篇文章不知道多少遍了,但就是沒有注意到下面這段話:

http://msdn.microsoft.com/en-us/library/ms171728.aspx

This exception occurs reliably during debugging and, under some circumstances, at run time. You might see this exception when you debug applications that you wrote with the .NET Framework prior to the .NET Framework version 2.0. You are strongly advised to fix this problem when you see it, but you can disable it by setting the CheckForIllegalCrossThreadCalls property to false. This causes your control to run like it would run under Visual Studio .NET 2003 and the .NET Framework 1.1.

At last, I think I should thanks Xiao Liang at the end of this blog article.

最后,我想在這篇blog文章的末尾再次感謝Xiao Liang。

轉載于:https://www.cnblogs.com/telnet_mike/archive/2011/10/11/2207655.html

總結

以上是生活随笔為你收集整理的非UI线程操作UI的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。