WinForm software, do not operate the UI in the thread

Source: Internet
Author: User

For WinForm Software, do not operate the UI in the thread, do not believe: Startform.checkforillegalcrossthreadcalls = false;

So, all the code is changed to the main thread delegate invocation way

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  updatesummary (string  Text) { Summaryinvokeifrequired (s =>  { S. Text = text });                /span>               

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 ();}    }

Call:

Richeditcontrol1.invokeifrequired (() =>{    //Do anything you want with the control    here Richeditcontrol1.rtftext = value;    Rtfhelpers.addmissingstyles (richEditControl1);});

Updated to:

public static void invokeifrequired (this isynchronizeinvoke obj,                                         methodinvoker action) {    if (obj. invokerequired) {        var args = new Object[0];        Obj. Invoke (action, args);    } else {        action ();}    }

Consider when an exception still occurs:

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);    }}

Change to:

public static void Invokeifrequired<t> (this T C, action<t> Action)     where T:control

Call Method:

Object1. Invokeifrequired (c + = {c.visible = true;});

WinForm software, do not operate the UI in the thread

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.