Form程式線程間調用其它線程式控制件

來源:互聯網
上載者:User

在一個Form程式中碰到一個問題,用一個定時器操作Form中的控制項,在Release時沒有反應,Debug時報錯。顯示為調用非本線程內控制項出錯。查詢資料發現必須這是非安全執行緒操作,用InvokeRequired來保護使它是安全執行緒的,就可以防止出錯。

Controls in Windows Forms are bound to a specific thread and are not thread safe. Therefore, if you are calling a control's method from a different thread, you must use one of the control's invoke methods to marshal the call to the proper thread. This property can be used to determine if you must call an invoke method, which can be useful if you do not know what thread owns a control.

(當表單控制項非安全執行緒時會綁定到指定的線程。當你通過其它線程來調用它時,必須用該控制項的調用方法來配置該調用進行排隊等候。如果你必須調用某方法InvokeRequired屬性可以用來確定當你不知道這個控制項現在是否可用。)

public delegate void MyCallBack();

public void MyFunction()
        {
            if (this.控制項名.InvokeRequired)
            {
                MyCallBack d = new MyCallBack(MyFunction);
                this.Invoke(d);
            }
            else
            {
                //進行操作
          }
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.