Winform軟體,不要線上程裡操作UI

來源:互聯網
上載者:User

標籤:code   int   ext   nes   線上   sharp   hand   ret   ttext   

對於Winform軟體,不要線上程裡操作UI,不要相信:StartForm.CheckForIllegalCrossThreadCalls = false;

於是,把所有的代碼都改成主線程委託調用的方式

private delegate void SetTextHandle(string id, string value);        private void ThreadSetText(string id, string value)        {            this.Controls.Find(id, true)[0].Text = value;        }        private void SetText(string id, string value)        {            if (this.InvokeRequired)            {                this.Invoke(new SetTextHandle(ThreadSetText), new object[] { id, value });            }            else            {                ThreadSetText(id, value);            }        }

2

// the canonical form (C# consumer)public delegate void ControlStringConsumer(Control control, string text);  // defines a delegate typepublic void SetText(Control control, string text) {    if (control.InvokeRequired) {        control.Invoke(new ControlStringConsumer(SetText), new object[]{control, text});  // invoking itself    } else {        control.Text=text;      // the "functional part", executing only on the main thread    }}

 3

public static class ControlHelpers{    public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : ISynchronizeInvoke    {        if (control.InvokeRequired)        {            control.Invoke(new Action(() => action(control)), null);        }        else        {            action(control);        }    }}

 

Use it like this:

private void UpdateSummary(string text){    summary.InvokeIfRequired(s => { s.Text = text });}

3
theLabel.Invoke(new Action(() => theLabel.Text = "hello world from worker thread!"));

 4

public static void InvokeIfRequired(this Control control, MethodInvoker action){    if (control.InvokeRequired) {        control.Invoke(action);    } else {        action();    }}

 調用:

richEditControl1.InvokeIfRequired(() =>{    // Do anything you want with the control here    richEditControl1.RtfText = value;    RtfHelpers.AddMissingStyles(richEditControl1);});

 更新為:

public static void InvokeIfRequired(this ISynchronizeInvoke obj,                                         MethodInvoker action){    if (obj.InvokeRequired) {        var args = new object[0];        obj.Invoke(action, args);    } else {        action();    }}

 考慮仍出現異常時:

while (!control.Visible){    System.Threading.Thread.Sleep(50);}


5
public static void InvokeIfRequired(this Control c, Action<Control> action){    if(c.InvokeRequired)    {        c.Invoke(new Action(() => action(c)));    }    else    {        action(c);    }}

 變更為:

public static void InvokeIfRequired<T>(this T c, Action<T> action)     where T : Control

 調用方法:

object1.InvokeIfRequired(c => { c.Visible = true; });

 

Winform軟體,不要線上程裡操作UI

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.