How to open a form in Thread

xiaoxiao2021-04-03  247

There are many friends who don't know how to open a form in Thread, always don't respond, let me talk about my own experience in this area. code show as below:

Private void threadfunc ()

{

// Run your code;

Form temp = new form ();

TEMP.SHOW ();

// Run your code;

}

Private void Button1_Click (Object Sender, System.Eventargs E)

{

FormthRead = New Thread (New Threadstart (threadfunc));

FormthRead.start ();

}

Like the code above, if you click the Button button, you can see that the created form flashes, it disappears, because you have built a form in the thread, all resources belong to this thread, so When this thread is over, its resources are also reclaimed, of course, C # will automatically turn the form to the form.

The correct way is to call with Invoke, the code is as follows:

Private void threadfunc ()

{

MethodInvoker Mi = New MethodInvoker (this.ShowForm);

THIS.BEGININVOKE (MI);

}

Private void showform ()

{

Form temp = new form ();

TEMP.SHOW ();

}

Private void Button1_Click (Object Sender, System.Eventargs E)

{

FormthRead = New Thread (New Threadstart (threadfunc));

FormthRead.start ();

}

转载请注明原文地址:https://www.9cbs.com/read-131674.html

New Post(0)