Ui background thread of winform

Source: Internet
Author: User

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.

 

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.