Asynchronously commission
Asynchronous entrustment provides the ability to call synchronization methods in asynchronous ways. When a commission is called synchronously, the call method calls the current thread to call the current thread. If the compiler supports asynchronous delegation, it will generate this calling method and the BeginInvoke and EndInvoke method. If the BeginInvoke method is called, the public language runtime will queue the request and return to the caller immediately. This goal is called for threads from the thread pool. The original thread of the submit request freely continues to perform parallel with the target method, which is to run the thread pool thread. If the callback has been specified for BeGinInvoke, it will call it when the target method returns. In the callback, use the EndInvoke method to get the return value and the input / output parameters. If you do not specify a callback for BeginInvoke, you can use endInvoke on the original thread submitted.
Use commission
For asynchronous programming, if the call uses a delegate, the call must define the delegate when calling a method. In the following code example, first define the delegate, then create the instance of the delegate, then call it. The following example illustrates that the calling party of the asynchronous calls define a mode:
Using system;
Using system.runtime.remoting;
Public Delegate Bool FactorizingCallback
Int FactorizAblenum,
Ref int primefactor1,
Ref int primefactor2;
// this is a class thing receives a callback when the results are available.
Public Class ProcessFactorizedNumber
{
Private Int _ulnumber;
Public ProcessFactorizedNumber (int Number)
{
_ULNUMBER = NUMBER;
}
// Note The Qualifier One-Way.
[OnewayATTRIBUTE ()]
Public Void FactorizedResults (IASYNCRESULT AR)
{
INT FACTOR1 = 0, Factor2 = 0;
// Extract the delegate from the asyncResult.
FactorizingCallback fd =
((AsyncResult) (asyncResult) ar) .asyncdelegate;
// Obtain the result.
FD.Endinvoke (Ref Factor1, Ref Factor2, Ar);
// Output results.
Console.writeline ("on callback: Factors of {0}: {1} {2}",
_ULNUMBER, FACTOR1, FACTOR2;
}
}
Asynchronous Variation 1 - Call
// The asynchronous variation 1 Call, Calls
// The processFactorizedNumber.FactorizedResults Callback
// when the call completion.
Public void factorizenumber1 ()
{
// The folload is the client code.
PrimeFactorizer Pf = New PrimeFactorizer ();
FactorizingCallback fd = new factory incent;
// asynchronous variation 1int factorizablenum = 1000589023, TEMP = 0;
// Create an instance of the class which is going
// to be caled when the call completion.
ProcessFactorizedNumber FC = New ProcessFactorizedNumber (FactorizAblenum);
// define the asyncCallback delegate.
AsyncCallback CB = New AsyncCallback (fc.factorizedResults);
// You can stuff any Object as the state object.
Object State = new object ();
// asynchronously invoke the factoryize method on pf.
IASYNCRESULT AR = fd.beginInvoke
FactorizAblenum,
Ref temp,
Ref temp,
CB,
State);
//
// Do some of the other.
//.
}
Asynchronous Variation 2
// asynchronous variation 2
// Waits for the result.
Public void factorizenumber2 ()
{
// The folload is the client code.
PrimeFactorizer Pf = New PrimeFactorizer ();
FactorizingCallback fd = new factory incent;
// asynchronous variation 1
INT factorizables = 1000589023, TEMP = 0;
// Create an instance of the class which is going
// TO Called When the Call Completes.
ProcessFactorizedNumber FC = New ProcessFactorizedNumber (FactorizAblenum);
// define the asyncCallback delegate.
AsyncCallback CB =
New asyncCallback (fc.factorizedResults);
// can stuff any Object as the state object.
Object State = new object ();
// asynchronously invoke the factoryize method on pf.
IASYNCRESULT AR = fd.beginInvoke
FactorizAblenum,
Ref temp,
Ref temp,
NULL,
NULL);
Ar.asyncwaithandle.waitone (10000, false);
IF (ar.iscompleted)
{
INT FACTOR1 = 0, Factor2 = 0;
// Obtain the result.
FD.Endinvoke (Ref Factor1, Ref Factor2, Ar);
// Output results.
Console.writeline ("Sequencial: Factors of {0}: {1} {2}",
FactorizAblenum, Factor1, Factor2;
}
Compiler and public language runtime support
When the compiler issues a FactorizingCallback commission class (after analyzing the definition as shown below), it will use asynchronous method signs and the Invoke method to generate the BeginInvoke and EndInvoke methods, as shown in the following code:
Public Class FactorizingCallback: DELEGATE
{
Public Bool Invoke
Int FactorizAblenum,
Ref int primefactor1,
Ref int primefactor2;
// The Following Code Was Supplied by The Compiler.
Public IASYNCRESULT BEGININVOKE
Int FactorizAblenum,
Ref int primefactor1,
Ref int primefactor2,
AsyncCallback CB,
Object asyncState
);
// The Following Code Was Supplied by The Compiler.
Public Bool endInvoke
Ref int primefactor1,
Ref int primefactor2,
IASYNCRESULT AR);
}
Entrusting BeginInvoke and endInvoke method provided by the compiler
Using the delegate signature specified by the user, the compiler should issue a delegate class with invoke, beginvoke, and endinvoke methods. BeginInvoke and EndInvoke methods should be modified as this unit. Because these methods are marked as native, the public language runtime automatically provides this implementation when the class is loaded. The loader ensures that they have not been overwritten.
Asynchronous entrustment programming example
The following code explains how to use .NET asynchronous program by solving some of the simple classes of certain digital factors.
Using system;
Using system.threading;
Using system.runtime.remoting;
// Create an asynchronous delegate.
Public Delegate Bool FactorizingCallback
Int FactorizAblenum,
Ref int primefactor1,
Ref int primefactor2;
// Create a class the factorizers the number.
Public Class PrimeFactorizer.
{
Public Bool Factorize
Int FactorizAblenum,
Ref int primefactor1,
Ref Int PrimeFactor2)
{
PrimeFactor1 = 1;
PrimeFactor2 = FactorizAblenum;
// Factorize Using A Low-Tech Approach.
For (int i = 2; i { IF (0 == (FactorizAblenum% i))) { PrimeFactor1 = i; PrimeFactor2 = FactorizAblenum / i; Break; } } IF (1 == PrimeFactor1) Return False; Else Return True; } } // Class That Receives A Callback When The The Results Are Available. Public class processfactorizednumber { Private Int _ulnumber; Public ProcessFactorizedNumber (int Number) { _ULNUMBER = NUMBER; } // Note The Qualifier is One-Way. [OnewayATTRIBUTE ()] Public Void FactorizedResults (IASYNCRESULT AR) { INT FACTOR1 = 0, Factor2 = 0; // Extract the delegate from the asyncResult. FactorizingCallback fd = ("asyncResult) ((asyncResult) ar) .asyncdelegate; // Obtain the result. FD.Endinvoke (Ref Factor1, Ref Factor2, Ar); // Output results. Console.writeline ("on callback: Factors of {0}: {1} {2}", _ULNUMBER, FACTOR1, FACTOR2; } } // Class That Shows Variations of Using Asynchronous Public class simple. { // The Following Demonstrates The Asynchronous Pattern Using A Callback. Public void factorizenumber1 () { // The folload is the client code. PrimeFactorizer Pf = New PrimeFactorizer (); FactorizingCallback fd = new factory incent; INT factorizables = 1000589023, TEMP = 0; // Create an instance of the class which is going // to be caled when the call completion. ProcessFactorizedNumber FC = New ProcessFactorizedNumber (FactorizAblenum); // define the asyncCallback delegate. AsyncCallback CB = New AsyncCallback (fc.factorizedResults); // you can stuff any Object as the state object Object State = new object (); // asynchronously invoke the factoryize method on pf. IASYNCRESULT AR = fd.beginInvoke FactorizAblenum, Ref temp, Ref temp, CB, State); // // Do some of the other. //. } // The folowing demonstrates The asynchronous pattern using a beginvoke, FOLLOWED by Waiting with a timeout. Public void factorizenumber2 () { // The folload is the client code. PrimeFactorizer Pf = New PrimeFactorizer (); FactorizingCallback fd = new factory incent; INT factorizables = 1000589023, TEMP = 0; // Create an instance of the class which is going // to be caled when the call completion. ProcessFactorizedNumber FC = New ProcessFactorizedNumber (FactorizAblenum); // define the asyncCallback delegate. AsyncCallback CB = New asyncCallback (fc.factorizedResults); // You can stuff any Object as the state object. Object State = new object (); // asynchronously invoke the factoryize method on pf. IASYNCRESULT AR = fd.beginInvoke FactorizAblenum, Ref temp, Ref temp, NULL, NULL); Ar.asyncwaithandle.waitone (10000, false); IF (ar.iscompleted) { INT FACTOR1 = 0, Factor2 = 0; // Obtain the result. FD.Endinvoke (Ref Factor1, Ref Factor2, Ar); // Output results. Console.writeline ("Sequencial: Factors of {0}: {1} {2}", FactorizAblenum, Factor1, Factor2; } } Public static void main (string [] args) { Simple Simple = New Simple (); Simple.Factorizenumber1 (); Simple.Factorizenumber2 (); } } to sum up The above is the basic concepts and sample code in VS.NET asynchronous programming, sorting out to everyone. If you have any suggestions, please Mil I Paulni@citiz.net.