| |

VerySource

 Forgot password?
 Register
Search
View: 572|Reply: 1

Asynchronous call

[Copy link]

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-28 14:20:01
| Show all posts |Read mode
// This is an example of an asynchronous call. SimpleMath has an asynchronous method called BeginAdd. Who can change the SimpleNath class to a form that contains beginxxx and endxxx methods, like asynchronous calls like beginxxx and endxxx in a socket.



using System;
using System.Runtime.Remoting.Messaging;
using System.Threading;

namespace DemoDelegate
{
public delegate int AddDelegate (int n1, int n2);
class Class1
{
static void Main (string [] args)
{
SimpleMath math = new SimpleMath ();
IAsyncResult asyncResult = math.BeginAdd (2,5, new AsyncCallback (AddCallback));
Console.WriteLine ("Press Enter to terminate the program ...");
Console.Read ();
}

// Addition callback function
private static void AddCallback (IAsyncResult ar)
{
AsyncResult async = (AsyncResult) ar;
AddDelegate d = (AddDelegate) async.AsyncDelegate;
int result = d.EndInvoke (ar);
Console.WriteLine ("2 + 5 =" + result);
Ranch
}
}

public class SimpleMath
{
// addition
public int Add (int n1, int n2)
{
Thread.Sleep (2000);
return n1 + n2;
}

// Addition using asynchronous processing
public System.IAsyncResult BeginAdd (int n1, int n2, AsyncCallback callback)
{
AddDelegate d = new AddDelegate (this.Add);
return d.BeginInvoke (n1, n2, callback, null);
}
}
}
Reply

Use magic Report

1

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-3-24 10:15:01
| Show all posts
In the CS program, when remoting peer-to-peer communication is used, if I do not use the event repeater mode, I directly transfer the set delegate to the server, and then the server implements the remote callback method in the callback client method. Is this method the same as two-machine communication? ?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list