Step by step by step by step: asynchronous operation

xiaoxiao2021-03-20  212

If you still don't know what is asynchronous, don't matter, we still come to see examples, and it is the most profound through instance. In Remoting, we can use the following asynchronous ways: 1, ordinary asynchronous 2, callback asynchronous 3, one-way asynchronous one, first we modify our remote object:

public

int

AlongTimeMethod

int

a,

int

b,

int

Time) {console.writeLine

"

Asynchronous method start

"

); System.threading.thread.sleep (time); console.writeline

"

End of asynchronous methods

"

);

Return

a

B;

This method passes two parameters, returns 2 parameters and representation of the method to perform success, and the method requires time millisecond execution time, which is a long-term method.

If we are called by asynchronous remote calls, you need to note that this method output is output instead of the client. Therefore, in order to test, we are still using local objects, before implementing asynchronous, let's take a look at the synchronous calling method, why is this a blocking? Because we call the method main thread waiting, look at the test:

Datetime DT

=

DateTime.Now; RemoteObject.myObject APP

=

New

RemoteObject.myObject (); console.writeline (app.alongtimeMethod "

1

,

2

,

1000

)); Method (); console.writeline

"

used

"

(Timespan) (DateTime.now)

-

DT)). Totalseconds

"

second

"

Console.readline ();

Assuming that the Method method is the main thread method, it takes 3 seconds:

Private

Static

Void

Method () {console.writeline

"

Main thread method start

"

); System.threading.thread.sleep

3000

Console.writeline

"

Main thread method ends

"

}

Ok, now start the program:

It took 4 seconds, indicating that the local use after our method is always waiting, the time used to go to the time = local method remote method, which is obviously unscientific for long-term methods! We need to improve:

1, ordinary asynchronous:

First, in front of the main method, the principal, signature, and return types are consistent with the asynchronous method.

Private

Delegate

int

MyDelegate

int

a,

int

b,

int

Time);

This is written in the main method:

Datetime DT

=

DateTime.Now; RemoteObject.myObject APP

=

New

RemoteObject.myObject (); MyDelegate MD

=

New

MyDelegate (app.alongtimeMethod); IASYNCRESULT IAR

=

Md.BeginInvoke (1

,

2

,

1000

,

NULL

,

NULL

Method ();

IF

(

!

Iar.iscompleted) {i.asyncwaithandle.waitone ();

Else

{Console.WriteLine

"

turn out

"

Md.Endinvoke (IAR));} console.writeline

"

used

"

(Timespan) (DateTime.now)

-

DT)). Totalseconds

"

second

"

Console.readline ();

Let's take a look at the results:

Now the total execution time is close to the execution time of the main thread, is equal to the calling method basically does not take time.

Analyze the code: IAR.asyncwaithandle.waitone (); is blocked waiting for the asynchronous method, this code is not executed, because the main method is completed, the asynchronous method has already been iScompleted, if we modify the code : IasyncResult IAR = md.beginInvoke (1,2,5000, null, null);

It can be seen that after the main thread method is ended, the asynchronous method is waiting to be completed, and the total of the total approach is close to the asynchronous method: 5 seconds.

In actual use, the main thread often needs to obtain the asynchronous method, that is, the case where the main thread does it work in the main thread, and will eventually wait for the result to wait for asynchronous operation. Thread operation. Looking at the second figure, you can find that asynchronous operation is only used for 1 second, but it is still not scientific after the main thread method is completed for 3 seconds. Therefore, we have to use the asynchronous technology of delegate callbacks.

2, the callback is asynchronous:

Class

Myclient {

Private

Delegate

int

MyDelegate

int

a,

int

b,

int

Time);

Private

Static

MyDelegate md; [stathread]

Static

Void

Main

String

[] Args) {datetime dt

=

Datetime.now;

//

RemoteObject.myObject App = (remoteObject.myObject) activator.getObject (TypeObject.myObject), System.configuration.configurationSettings.AppSettings ["ServiceURL"]);

RemoteObject.myObject APP

=

New

RemoteObject.myObject (); md

=

New

MyDelegate (app.alongtimeMethod); asyncCallback AC

=

New

AsyncCallback (MyClient.callback); IASYNCRESULT IAR

=

Md.BeginInvoke

1

,

2

,

1000

, AC,

NULL

); Method (); console.writeline

"

used

"

(Timespan) (DateTime.now)

-

DT)). Totalseconds

"

second

"

Console.readline ();

public

Static

Void

Callback (IASYNCRESULT IAR) {

IF

(Iar.iscompleted) {console.writeLine

"

turn out

"

Md.Endinvoke (IAR);}}

Private

Static

Void

Method () {console.writeline

"

Main thread method start

"

); System.threading.thread.sleep

3000

Console.writeline

"

Main thread method ends

"

}}

You can see the comment on my comment, remove the comment from the remote call, call the comment on the following, start the server, and then start the client is a remote call.

The asynchronous call is over, and the result can be displayed immediately. If the remote method is turned on, you can see more clear: Client: Main thread method start - "server: asynchronous method -" server: End of asynchronous method - "client : The result is 3- "Client: The main thread method ends -" Client: used for 3.03125 seconds. 3, one-way asynchronous as a method of calling the method as synchronous call, but the method is completed, but the return value of the method is not obtained and the abnormal information of the called method is not obtained like a synchronization method! For long-term methods that do not need to return information, we can let it go to do it: Remote objects:

Using

System;

Using

System.Runtime.Remoting.MESSAGING;

Namespace

REMOTEOBJECT {

public

Class

MyObject: MarshalByrefObject {[Oneway]

public

Void

AlongTimeMethodoneway (

int

Time) {console.writeLine

"

Asynchronous method start

"

); System.threading.thread.sleep (time); console.writeline

"

End of asynchronous methods

"

}}}

[Oneway] Property is part of Remoting.Messaging, don't forget Using, look at the client code:

Using

System;

Namespace

RemoteClient {

Class

MyClient {[stathread]

Static

Void

Main

String

[] Args) {datetime dt

=

DateTime.Now; RemoteObject.myObject APP

=

(RemoteObject.myObject) activator.getObject (

Typeof

(RemoteObject.myObject), System.configuration.configurationSettings.AppSettings ["

ServiceURL

"

]);

//

RemoteObject.myObject App = New RemoteObject.myObject ();

app.alongtimemethodoneway (

1000

); Method (); console.writeline

"

used

"

(Timespan) (DateTime.now)

-

DT)). Totalseconds

"

second

"

Console.readline ();

Private

Static

Void

Method () {console.writeline

"

Main thread method start

"

); System.threading.thread.sleep

3000

Console.writeline

"

Main thread method ends

"

}}}

This time we can only debug in remote debugging, let's let the asynchronous method to do, then you will be worried about the main thread, other no matter. Run the results I describe: Client: Main thread method start - "server: Asynchronous method -" server: Asynchronous method end - "Client: Main thread method end -" client: used for 3.8 seconds. The three methods mentioned above are only part of asynchronous programming. How often is the remote method to combine the actual example to see if the return of the method is needed to use the runtime operation time and the remote method runtime. For example, WaitHandle can also be implemented in polling: while (IAR.iscompleted == false) system.threading.thread.sleep (10); generally, remote object's asynchronous operation and local object's asynchronous operation is very close . You can also refer to MSDN Related Articles: http://msdn.microsoft.com/library/chs/default.asp? URL = / library / coup / cpguide / html / cpconasynchronousprogrammingDesignPattern2.asp

转载请注明原文地址:https://www.9cbs.com/read-130423.html

New Post(0)