Multithreading is an old topic. Today I will talk about the most basic threads, UI threads and background threads.
An exception occurs when you directly operate the UI control in the background thread (the Inter-thread operation is invalid: access it from a thread that does not create the control "XX ). How can this problem be solved? The key points are in the proxy and invokerequired attributes, and the winform UI (window)CodeAs follows:
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. text;
Using System. Windows. forms;
UsingLandpylibrary;
UsingLandpycommon;
namespace landpywindowsform
{< br> Public partial class mainform: form, iformworkthread
{< br> Public mainform ()
{< br> initializecomponent ();
cdobj = settime;
}
private DeleGate void controldelegate ( string time );
private workthread wtobj;
controldelegate cdobj;
Private VoidSettime (StringTime)
{
Lbltime. Text=Time;
}
Public Void Showtime ( String Time)
{
If (Lbltime. invokerequired)
{
Invoke (cdobj, time );
}
Else
{< br> settime (time);
}< BR >}
Private VoidBtnstart_click (ObjectSender, eventargs E)
{
Wtobj= NewWorkthread (This);
Wtobj. startshowtime ();
}
Private Void Btnend_click ( Object Sender, eventargs E)
{
If (Wtobj ! = Null )
{
Wtobj. endshowtime ();
}
}
Private Void Mainform_formclosing ( Object Sender, formclosingeventargs E)
{
If (Wtobj ! = Null )
{
Wtobj. endshowtime ();
}
}
}
}
To operate the UI thread in the background thread, our mainform inherits the public interface iformworkthread. The interface definition is as follows:
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace landpycommon
{
Public interface iformworkthread
{
Void Showtime (string time );
}
}
This interface is placed in an independent DLL, And the UI thread and background thread are both in differentProgramAnd they all reference the DLL of this interface.
The background Thread class is as follows:
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Threading;
UsingLandpycommon;
NamespaceLandpylibrary
{
Public ClassWorkthread
{
PrivateIformworkthread _ formworkthread;
PrivateThread thread;
PublicWorkthread (iformworkthread formworkthread)
{
_ Formworkthread=Formworkthread;
}
Public Void Deal ()
{
While ( True )
{
Thread. Sleep ( 1000 );
_ Formworkthread. Showtime (datetime. Now. tostring ());
}
}
Public Void Startshowtime ()
{
Thread = New Thread ( New Threadstart (deal ));
Thread. Name = " Timethread " ;
Thread. Start ();
}
Public VoidEndshowtime ()
{
Thread. Abort ();
}
}
}
In fact, an exception is reported because the multi-threaded UI control is insecure. Ms provides a solution. The key code is as follows:
Public mainform ()
{
Initializecomponent ();
Cdobj = settime;
}
Private delegate void controldelegate (string time );
Controldelegate cdobj;
Private void settime (string time)
{
Lbltime. Text = time;
}
Public void Showtime (string time)
{
If (lbltime. invokerequired)
{
Invoke (cdobj, time );
}
Else
{
Settime (time );
}
}
This problem is solved through the proxy and invokerequired attributes.