C # multi-thread programming: asynchronous event calling

Source: Internet
Author: User

When an event is triggered, the method for subscribing to the event is executed in the thread that triggers the event. That is to say, the method for subscribing to this event is synchronously executed in the thread that triggers the event. Therefore, there is a problem: if the method to subscribe to an event takes a long time to execute, the thread that triggers the event is blocked, and the method execution is completed for a long time. In this way, not only the execution of subsequent subscription event methods is affected, but also the primary thread responds to other user requests in a timely manner. How can this problem be solved? At this point, I think you have come up with an asynchronous event call.

How to Implement Asynchronous event calling? If you know more about events, you should know that the nature of events is actually a multicastdelegate ). The multicastdelegate class provides a getinvocationlist method, which returns the array of delegate calls of this multicast delegate. This method can be used to implement the asynchronous event calling function.

Sample Code:

Using system;
Using system. Threading;
Using system. runtime. remoting. messaging;

Namespace processtest
{
Class Program
{
// Define an event
Public static event eventhandler <eventargs> onevent;

// Method 1
Static void Method1 (Object sender, eventargs E)
{
// Display the thread ID for executing this method
Console. writeline ("the thread ID for calling Method1 is: {0}", thread. currentthread. managedthreadid );
Thread. Sleep (1000 );
}

// Method 2
Static void method2 (Object sender, eventargs E)
{
Console. writeline ("the thread ID for calling method2 is: {0}", thread. currentthread. managedthreadid );
Thread. Sleep (1000 );
}

Static void main (string [] ARGs)
{
// Display the main thread ID
System. Console. writeline ("main thread ID: {0}", thread. currentthread. managedthreadid );

// Register Method1 and method2 to the event
Onevent + = new eventhandler <eventargs> (Method1 );
Onevent + = new eventhandler <eventargs> (method2 );

// The following code implements asynchronous calls to events
// Obtain the multi-channel delegate list in the event
Delegate [] delegary = onevent. getinvocationlist ();
// Traverse the delegate list
Foreach (eventhandler <eventargs> Deleg in delegary)
{
// Asynchronous call delegate
Deleg. begininvoke (null, eventargs. Empty, null, null );
}

System. Console. readkey ();
}
}
}

The code execution result is as follows:

  

Observe the running results and you will find that the main thread ID, the thread ID that executes the Method1 function, and the thread ID that executes the method2 function are different. Therefore, we can see that asynchronous event calls are implemented.

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.