欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
C#跨線(xiàn)程更改Form中控件的屬性
 

WindowsForms 控件通常不是thread-safe(直接或間接繼承于System.Windows.Forms.Control),因此.NETFramework為防止multithread下對控件的存取可能導致控件狀態(tài)的不一致,在調試時(shí),CLR-Debugger會(huì )拋出一個(gè)InvalidOperationException以‘建議‘程序員程序可能存在的風(fēng)險。
 
問(wèn)題的關(guān)鍵在于,動(dòng)機是什么?和由此而來(lái)的編程模型的調整。
首先,看一個(gè)代碼實(shí)例。該例要完成的工作是由一個(gè)Button的Click觸發(fā),啟動(dòng)一個(gè)Thread(Manual Thread),該Thread的目的是完成設置TextBox的Text’s Property。
 
Code 1.1
using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
 
namespace WindowsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
 
        private void unsafeSetTextButton_Click(object sender, EventArgs e) {
            Thread setTextThread = new Thread(new ThreadStart(doWork));
            setTextThread.Start();
        }
 
        private void doWork() {
            string fileName = ".\\test-src.txt";
            if (!File.Exists(fileName)) {
                MessageBox.Show(string.Format("{0} doesn‘t exist!", fileName),
                    "FileNoFoundException");
                return;
            }
 
            string text = null;
            using (StreamReader reader = new StreamReader(fileName, Encoding.Default)) {
                text = reader.ReadToEnd();
            }
 
            this.textBox1.Text = text;
        }
    }
}
 
在調試時(shí),CLR-Debugger會(huì )在以上代碼中粗體處將會(huì )彈出如下的對話(huà)框:
 
提示說(shuō),當前存取控件的thread非創(chuàng )建控件的thread(Main Thread)。
 
 
當然,你也可以忽略InvalidOperationException,在非調試的狀態(tài)下,該異常并不會(huì )被拋出,CLR-Debugger監測對Handle的可能存在的不一致地存取,而期望達到更穩健(robust)的代碼,這也就是Cross-thread operation notvalid后的真正動(dòng)機。
 
但是,放在面前的選擇有二:第一,在某些情況下,我們并不需要這種善意的‘建議‘,而這種建議將在調試時(shí)帶來(lái)了不必要的麻煩;第二,順應善意的‘建議‘,這也意味著(zhù)我們必須調整已往行之有效且得心應手的編程模型(成本之一),而這種調整額外還會(huì )帶來(lái)side-effect,而這種side-effect目前,我并不知道有什么簡(jiǎn)潔優(yōu)雅的解決之道予以消除(成本之二)。
 
忽略Cross-thread InvalidOperationException建議,前提假設是我們不需要類(lèi)似的建議,同時(shí)也不想給自己的調試帶來(lái)過(guò)多的麻煩。
 
關(guān)閉CheckForIllegalCrossThreadCalls,這是Control class上的一個(gè)staticproperty,默認值為flase,目的在于開(kāi)關(guān)是否對Handle的可能存在的不一致存取的監測;且該項設置是具有Applicationscope的。
 
如果,只需要在某些Form中消除Cross-threadInvalidOperationException建議,可以在Form的.ctor中,InitializeComponent語(yǔ)句后將CheckForIllegalCrossThreadCalls設置為false 。
 
Code 2. - 1
public Form1() {
    InitializeComponent();
 
    Control.CheckForIllegalCrossThreadCalls = false;
}
 
這種方式雖然可以達到忽略Cross-thread InvalidOperationException建議的目的,但代碼不能明晰的表達具有Application scope的語(yǔ)義,下面方式能更好的表達Application scope語(yǔ)義而且便于維護。
 
Code 2. - 2
static void Main() {
    Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
 
Control.CheckForIllegalCrossThreadCalls = false;
 
    Application.Run( new Form1() );
}
 
 
接受Cross-thread InvalidOperationException善意的建議,這通常是個(gè)明智的選擇,即使目前沒(méi)有簡(jiǎn)潔優(yōu)雅的code pattern。
 
Code 3. – 1
using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
 
namespace WindowsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
 
            //Control.CheckForIllegalCrossThreadCalls = false;
        }
 
        private void safeSetTextButton_Click(object sender, EventArgs e) {
            Thread safeSetTextThread = new Thread(new ThreadStart(doWork));
            safeSetTextThread.Start();
        }
 
        private void doWork() {
            string fileName = ".\\test-src.txt";
            if (!File.Exists(fileName)) {
                MessageBox.Show(string.Format("{0} doesn‘t exist!", fileName),
                    "FileNoFoundException");
                return;
            }
 
            string text = null;
            using (StreamReader reader = new StreamReader(fileName, Encoding.Default)) {
                text = reader.ReadToEnd();
            }
 
            //this.textBox1.Text = text;
            safeSetText(text);
        }
 
        private void safeSetText(string text) {
            if (this.textBox1.InvokeRequired) {
                _SafeSetTextCall call = delegate(string s) {
                    this.textBox1.Text = s;
                };
 
                this.textBox1.Invoke(call, text);
            }
            else
                this.textBox1.Text = text;
        }
 
        private delegate void _SafeSetTextCall(string text);
    }
}
其中主要利用System.ComponentModel.IsynchronizeInvoke的InvokeRequired和Invoke方法(System.Windows.Forms.Control繼承于此),該codepattern對于大多數Windows控件有效 ;這樣做的目的是保證由創(chuàng )建控件的MainThread唯一性地呼叫g(shù)et_Handle。(注意Code 3. -1 中的粗體 safeSetText方法)
 
但,System.Windows.Forms中ToolStripItem繼承鏈上的控件并不具有后向兼容性,因此以上code pattern對此類(lèi)控件不適用;可以將以上code pattern改為如下:
        private void safeSetText(string text) {
            if (this.InvokeRequired) {
                _SafeSetTextCall call = delegate(string s) {
                    this.textBox1.Text = s;
                };
 
                this.Invoke(call, text);
            }
            else
                this.textBox1.Text = text;
        }
 
        private delegate void _SafeSetTextCall(string text);
 
因為System.Windows.Form繼承System.Windows.Control,可以保證以上代碼可以正確編譯也能正常按期望工作,這樣一來(lái),代碼的彈性會(huì )好些。
 
國外有兄弟利用Reflection技術(shù)將設置單一屬性(Property)完全動(dòng)態(tài)化了,代碼的彈性因此也更好,但我不鼓勵這種做法。理由有二:第一,之所以采用Multithread是因為需要更好的UI反應(interactive)、或者更好的性能、或者兩者都要,在這種前提下,Reflection似乎與目標背道而馳;第二,目前這種實(shí)現技術(shù)所帶來(lái)的代碼彈性的提升非常有限;不過(guò)有興趣的,可以自己驗證一下。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
c#serialport類(lèi)實(shí)現串口通信的源代碼
C#中的跨線(xiàn)程調用
線(xiàn)程間操作無(wú)效: 從不是創(chuàng )建控件“...”的線(xiàn)程訪(fǎng)問(wèn)它。
子線(xiàn)程訪(fǎng)問(wèn)住窗體控件
文本框小部件TextBox——WindowsForm系列教程
.NET開(kāi)發(fā)中的一些小技巧 - 團團的園子 - 博客園
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久