Asynchronous UI operations in multi-threaded Environments

Source: Internet
Author: User

Solve the problem of non-interworking among threads in vs2005, a package of solutions:

1. First, define a class: setcontrolproperty

 
UsingSystem. reflection;
 
UsingSystem;
 
UsingSystem. Windows. forms;
 
 
NamespaceWays. utils
 
{
 
ClassSetcontrolproperty
 
{
Delegate VoidSetvaluedelegate (Object OBJ, object Val, object [] index );
 
 
 
PublicSetcontrolproperty (control Ctrl, string propname, object Val)
 
{
Propertyinfo propinfo = ctrl. GetType (). getproperty (propname );
 
Delegate dgtsetvalue =NewSetvaluedelegate (propinfo. setvalue );
 
CTRL. Invoke (dgtsetvalue,NewObject [3] {Ctrl, Val,Null});
 
}
}
 
}

2. Call it in the form to be operated

In this example, this call is triggered by a communication event:

 
VoidTestcommand_endstatusevent (ObjectSender, eventargs E)
 
{
NewSetcontrolproperty (label4 ,"Text","End");
 
NewSetcontrolproperty (button1 ,"Enabled",True);
 
}

Iii. simplified but insecure Solution

Control. checkforillegalcrossthreadcils =False;

I tried it. In. NET Compact framework, it is unavailable!

Iv. Solution in. netcf. Source: Ui asynchronous refresh in. NET Compact framework multi-threaded Environment

In the development of wince or Windows Mobile, you usually need to submit some tasks to the worker thread. When the status of the worker thread changes, you need to notify the UI process to refresh the UI, for example, a network connectionProgramThe worker thread is responsible for managing connections such as WiFi, GPRS, or 3G. When the connection status changes, the worker thread notifies the UI thread of the update status, while the UI thread updates the UI to notify the user.

I often have a question about why worker thread does not directly update the UI, Which is simpler and clearer. However, the UI refresh is NOT thread safe, so the worker thread directly updates the UI and throws the "cross-thread operation not valid" exception. So the thread safe notification method is required. The following shows how to update the message:
Worker Thread class

ClassConnectionmgr
 
{
 
// Delegate for message
 
Public Delegate VoidMessageeventhandler (StringMSG );
Public EventMessageeventhandler messageevent;
 
 
 
// The delegate of message event
 
Private VoidMessagehandler (StringMSG)
{
 
Messageeventhandler messageevent = messageevent;
 
If(Messageevent! =Null)
 
{
 
Messageevent (MSG );
}
 
}
 
Private VoidConnecthandler ()
 
{
 
Messagehandler ("Connected");
}
 
Private VoidDisconnecthandler ()
 
{
 
Messagehandler ("Disconnected");
 
}
}

Define delegate and event in worker thread for UI thread registration. The delegate is called when the status changes.

Ui thread

 
PublicPartialClassForm1: Form
 
{
 
PublicForm1 ()
{
 
// Register the connect event
 
Connectionmgr. instance. messageevent + = messageevent;
 
}
 
 
Private VoidMessageevent (StringMSG)
 
{
 
Safewinformsthreaddelegate d =NewSafewinformsthreaddelegate (showmessage );
Invoke (D,New Object[] {MSG });
 
}
 
 
 
Public Delegate VoidSafewinformsthreaddelegate (StringMSG );
Private VoidShowmessage (StringMSG)
 
{
 
Eventtext. Text = MSG;
 
}
 
}

Ui thread subscribes to Connection events through Delegate. When the connection status changes, worker thread asynchronously calls void messageevent (string MSG ). Here, the invoke method is called to ensure thread security. The call parameters are transmitted using object [], so the program can transmit any information, and the UI can render any information as long as the worker thread can provide.

Reference control. Invoke method in msdn

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.