乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C#跨線程更改Form中控件的屬性

       ShangShujie 2008-01-22
       

      Windows Forms 控件通常不是thread-safe(直接或間接繼承于System.Windows.Forms.Control),因此.NET Framework為防止multithread下對(duì)控件的存取可能導(dǎo)致控件狀態(tài)的不一致,在調(diào)試時(shí),CLR-Debugger會(huì)拋出一個(gè) InvalidOperationException以‘建議‘程序員程序可能存在的風(fēng)險(xiǎn)。
       
      問題的關(guān)鍵在于,動(dòng)機(jī)是什么?和由此而來的編程模型的調(diào)整。
      首先,看一個(gè)代碼實(shí)例。該例要完成的工作是由一個(gè)Button的Click觸發(fā),啟動(dòng)一個(gè)Thread(Manual Thread),該Thread的目的是完成設(shè)置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;
              }
          }
      }
       
      在調(diào)試時(shí),CLR-Debugger會(huì)在以上代碼中粗體處將會(huì)彈出如下的對(duì)話框:
       
      提示說,當(dāng)前存取控件的thread非創(chuàng)建控件的thread(Main Thread)。
       
       
      當(dāng) 然,你也可以忽略InvalidOperationException,在非調(diào)試的狀態(tài)下,該異常并不會(huì)被拋出,CLR-Debugger監(jiān)測對(duì) Handle的可能存在的不一致地存取,而期望達(dá)到更穩(wěn)健(robust)的代碼,這也就是Cross-thread operation not valid后的真正動(dòng)機(jī)。
       
      但是,放在面前的選擇有二:第一,在某些情況下,我們并不需要這種善意的‘建議 ‘,而這種建議將在調(diào)試時(shí)帶來了不必要的麻煩;第二,順應(yīng)善意的‘建議‘,這也意味著我們必須調(diào)整已往行之有效且得心應(yīng)手的編程模型(成本之一),而這種 調(diào)整額外還會(huì)帶來side-effect,而這種side-effect目前,我并不知道有什么簡潔優(yōu)雅的解決之道予以消除(成本之二)。
       
      忽略Cross-thread InvalidOperationException建議,前提假設(shè)是我們不需要類似的建議,同時(shí)也不想給自己的調(diào)試帶來過多的麻煩。
       
      關(guān) 閉CheckForIllegalCrossThreadCalls,這是Control class上的一個(gè)static property,默認(rèn)值為flase,目的在于開關(guān)是否對(duì)Handle的可能存在的不一致存取的監(jiān)測;且該項(xiàng)設(shè)置是具有Application scope的。
       
      如果,只需要在某些Form中消除Cross-thread InvalidOperationException建議,可以在Form的.ctor中,InitializeComponent語句后將 CheckForIllegalCrossThreadCalls設(shè)置為false 。
       
      Code 2. - 1
      public Form1() {
          InitializeComponent();
       
          Control.CheckForIllegalCrossThreadCalls = false;
      }
       
      這種方式雖然可以達(dá)到忽略Cross-thread InvalidOperationException建議的目的,但代碼不能明晰的表達(dá)具有Application scope的語義,下面方式能更好的表達(dá)Application scope語義而且便于維護(hù)。
       
      Code 2. - 2
      static void Main() {
          Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault( false );
       
      Control.CheckForIllegalCrossThreadCalls = false;
       
          Application.Run( new Form1() );
      }
       
       
      接受Cross-thread InvalidOperationException善意的建議,這通常是個(gè)明智的選擇,即使目前沒有簡潔優(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繼承于此),該code pattern對(duì)于大多數(shù)Windows控件有效 ;這樣做的目的是保證由創(chuàng)建控件的Main Thread唯一性地呼叫g(shù)et_Handle。(注意Code 3. -1 中的粗體 safeSetText方法)
       
      但,System.Windows.Forms中ToolStripItem繼承鏈上的控件并不具有后向兼容性,因此以上code pattern對(duì)此類控件不適用;可以將以上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);
       
      因?yàn)镾ystem.Windows.Form繼承System.Windows.Control,可以保證以上代碼可以正確編譯也能正常按期望工作,這樣一來,代碼的彈性會(huì)好些。
       
      國 外有兄弟利用Reflection技術(shù)將設(shè)置單一屬性(Property)完全動(dòng)態(tài)化了,代碼的彈性因此也更好,但我不鼓勵(lì)這種做法。理由有二:第一,之 所以采用Multithread是因?yàn)樾枰玫腢I反應(yīng)(interactive)、或者更好的性能、或者兩者都要,在這種前提下, Reflection似乎與目標(biāo)背道而馳;第二,目前這種實(shí)現(xiàn)技術(shù)所帶來的代碼彈性的提升非常有限;不過有興趣的,可以自己驗(yàn)證一下。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多