C # two simple mechanisms for implementing asynchronous programming

Source: Internet
Author: User

Understanding the differences among programs, processes, and threads: in short, a program has at least one process, and a process has at least one thread.
A process is a program running in the memory (that is, a running program); a process generally has only one thread, and a process can contain multiple threads (multi-thread programming );

Simple Mechanism for asynchronous programming 1: asynchronous Delegation
The ininvoke and endinvoke methods of the delegate type.
Begininvoke method:
Parameter composition: list of parameters for Reference Methods + callback parameters + state parameters
Returned value: iasyncresult interface type (Interface Description: This interface has two important attributes: iscompleted attribute bool type indicates whether the asynchronous thread (method) is executed completely; asyncstate.
This interface is a member of the asyncresult class. This class also has an important member: asyncdeleget returns the reference of the called deleGATE)

What is done when the method is called:
1. Get a thread in the thread pool. When the thread is scheduled, execute the delegate list method;
2. Return a reference to an iasyncresult object to the main thread;

Endinvoke method:
Parameter composition: iasyncresult type
Return Value: Type of the returned value of the called deleGATE

What is done when the method is called:
Locate the associated thread according to the parameter: iasyncresult object,
If this thread has been executed, eedinvoke will do the following: clean up the state and resources of the thread; find the return value of the referenced method and return it as its own return value.
If this thread is not completed, the calling thread stops and waits, knowing that the cleaning is complete and the return value is returned.


Three Modes of asynchronous programming: waiting till completion mode, polling mode, and callback mode.
The following is a callback mode column:
Public class testthreading
{
Public static int method (int n, int m)
{
Console. writeline ("Asynchronous Method starts internally ");
System. Threading. thread. Sleep (5000 );
Return n> M? N: m;
}

Public static void callback (iasyncresult IA)
{

Asyncresult AR = (asyncresult) IA;
MYDEL del = (MYDEL) Ar. asyncdelegate;
Int result = del. endinvoke (IA );
Console. writeline (result );

}
}

Static void main (string [] ARGs)
{
# Region asynchronous programming and thread

Console. writeline ("processing code in main ..... ");
MYDEL del = new MYDEL (testthreading. method );
Console. writeline ("before the begininvoke method is called ");
Iasyncresult IA = del. begininvoke (32,100, testthreading. Callback, null );
Console. writeline ("after begininvoke is called, main continues to do other things ");
System. Threading. thread. Sleep (5000 );
Console. writeline ("main program ended ");

# Endregion
}

Simple Mechanism of asynchronous programming 2: Timer
. Net Bcl provides several timer classes. Here I will only introduce timer in system. threading.
First, let's take a look at the most common constructors of Timer:
Timer (timercallback callback, objec state, int duetime, int period)

The following describes the parameters respectively. After the introduction, you will use timer to implement asynchronous programming:
Timercallback is a delegate type and is defined as void timercallback (Object State). This is the callback function. The timer executes the callback method at each time.
State is the parameter to be passed to the callback method.
Duetime is the time before the first execution of the callback method.
Period is the interval between two callback methods.

The demo is as follows:

Public class testtimer
{
Public static void timercallback (Object O)
{
Console. writeline ("{0}, {1}", O, datetime. Now. tostring ("YY: mm: dd hh: mm: SS "));
}
}

Static void main (string [] ARGs)
{

# Use of the region Timer class (under system. Threading) Use timer to send asynchronous (callback function)

Timer time = new timer (testtimer. timercallback, "lxf", 5000,100 0 );

# Endregion
}

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.