C # Async callback function

Source: Internet
Author: User

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;


Namespace comprehensivetest.com
{
public class AsyCallEx112
{
Defines a delegate that performs an addition
public delegate int sum (int a, int b);
public class number
{
public int m = 4;
public int Numberadd (int a, int b)
{
int C = a + B;
return C;
}
Define a with. NET Framework-defined callback function for the AsyncCallback delegate
public void CallbackMothed2 (IAsyncResult ar2)
{
sum s = (sum) ar2. asyncstate;
int number = S.endinvoke (AR2);
m = number;
Console.WriteLine ("M value obtained: {0}", m);
}
}
}
}


Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;


Namespace comprehensivetest.com
{
public class AsyCallEx113
{
Define a delegate
public delegate void Asynceventhanlder ();
public class Class1
{
public void Event1 ()
{
Console.WriteLine ("Event1 start");
System.Threading.Thread.Sleep (2000);
Console.WriteLine ("Event1 End");
}
public void Event2 ()
{
Console.WriteLine ("Event2 start");
int i = 1;
while (I < 100)
{
i = i + 1;
Console.WriteLine ("Event2" + i.tostring ());
}
Console.WriteLine ("Event2 End");
}
public void Callbackmethod (IAsyncResult ar)
{
((Asynceventhanlder) ar. asyncstate). EndInvoke (AR);
}
}
}
}



Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Comprehensivetest.com;


Namespace Comprehensivetest
{
Class Program
{
static void Main (string[] args)
{
112
Asycallex112.number num = new Asycallex112.number ();
Asycallex112.sum numberadd= New Asycallex112.sum (Num.numberadd);
AsyncCallback numberback = new AsyncCallback (Num. CALLBACKMOTHED2);
Numberadd. BeginInvoke (+, Numberback, numberadd);
Console.WriteLine ("Results obtained:");
Console.WriteLine (NUM.M);


113
Long start = 0;
Long end = 0;
Asycallex113.class1 C = new Asycallex113.class1 ();
Console.WriteLine ("Ready");
start = DateTime.Now.Ticks;


Asycallex113.asynceventhanlder asy = new Asycallex113.asynceventhanlder (C.EVENT1);
IAsyncResult ia = asy. BeginInvoke (null, NULL);


IAsyncResult ia = asy. BeginInvoke (New AsyncCallback (C.callbackmethod), asy);
Ia. Asyncwaithandle.waitone ();
C.event2 ();

end = DateTime.Now.Ticks;
Console.WriteLine ("Timescale difference =" + convert.tostring (End-start));
Console.ReadLine ();
}
}
}


This article is from the "Better_power_wisdom" blog, make sure to keep this source http://aonaufly.blog.51cto.com/3554853/1698947

C # Async callback function

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.