/* 异步调用; 线程在调用方法后立即返回而不去等待调用完成,尤其是在以异步方式管理数据流或在管理对处在另一个AppDomain中的远程对象的异步调用的时候。 在异步调用期间,不用创建和管理用于执行方法的线程,该线程由线程池管理。*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace AsyncDemo
{
class AsyncDelegateDemo
{
public delegate int Deleg(int a, int b);
static int Sum(int a, int b)
{
int sum = a + b;
Console.WriteLine("Thread#{0}:sum() sum={1}", Thread.CurrentThread.ManagedThreadId, sum);
return sum;
}
static void Main(string[] args)
{
Deleg proc = Sum;
IAsyncResult async = proc.BeginInvoke(10, 15, null, null);
//you can do some work here
for (int i = 0; i < 10000; i++)
{
if (i % 1000 == 0)
Console.WriteLine("the num is {0}", i.ToString());
}
int sum = proc.EndInvoke(async);
Console.WriteLine("Thread#{0}:main() sum={1}", Thread.CurrentThread.ManagedThreadId, sum);
}
}
}
/*回调方法:
* 为异步调用指定一个方法,该方法将在异步调用结束的时候被自动调用。
* 注意:
* 如果执行方法的线程处于后台,那么可能会出现问题。我们必须引入事件管理机制来确保应用程序不会在异步调用的执行尚未完成的时候就终止了。
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace AsyncDemo
{
class CallBackMethod
{
public delegate int Deleg(int a, int b);
//Initial state of the event;fasle
static AutoResetEvent ev = new AutoResetEvent(false);
static int Sum(int a, int b)
{
int sum = a + b;
Console.WriteLine("Thread#{0}:sum() sum={1}", Thread.CurrentThread.ManagedThreadId, sum);
return sum;
}
static void SumDone(IAsyncResult async)
{
//wait a second to simulate some work
Thread.Sleep(1000);
Deleg proc = ((AsyncResult)async).AsyncDelegate as Deleg;
int sum = proc.EndInvoke(async);
Console.WriteLine("Thread#{0}:callback() sum={1}", Thread.CurrentThread.ManagedThreadId, sum);
ev.Set();
}
static void Main()
{
Deleg proc = Sum;
IAsyncResult async = proc.BeginInvoke(10, 15, SumDone, null);
Console.WriteLine("Thread#{0}:BeginInvoke() called,wait for sumdone() completion.", Thread.CurrentThread.ManagedThreadId);
// you can do some work here
for (int i = 0; i < 10000; i++)
{
if (i % 1000 == 0)
Console.WriteLine("the num is {0}", i.ToString());
}
ev.WaitOne();
//for (int i = 0; i < 10000; i++)
//{
// if (i % 1000 == 0)
// Console.WriteLine("the num is {0}", i.ToString());
//}
Console.WriteLine("{0}:bye...", Thread.CurrentThread.ManagedThreadId);
Console.Read();
}
}
}
//向回调方法传递状态
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace AsyncDemo
{
class CallBackMethod
{
public delegate int Deleg(int a, int b);
//Initial state of the event;fasle
//static AutoResetEvent ev = new AutoResetEvent(false);
static int Sum(int a, int b)
{
int sum = a + b;
Console.WriteLine("Thread#{0}:sum() sum={1}", Thread.CurrentThread.ManagedThreadId, sum);
return sum;
}
static void SumDone(IAsyncResult async)
{
//wait a second to simulate some work
Thread.Sleep(1000);
Deleg proc = ((AsyncResult)async).AsyncDelegate as Deleg;
int sum = proc.EndInvoke(async);
Console.WriteLine("Thread#{0}:callback() sum={1}", Thread.CurrentThread.ManagedThreadId, sum);
((AutoResetEvent)async.AsyncState).Set();
}
static void Main()
{
Deleg proc = Sum;
//向回调方法传递状态
AutoResetEvent ev = new AutoResetEvent(false);
IAsyncResult async = proc.BeginInvoke(10, 15, SumDone, ev);
Console.WriteLine("Thread#{0}:BeginInvoke() called,wait for sumdone() completion.", Thread.CurrentThread.ManagedThreadId);
// you can do some work here
for (int i = 0; i < 10000; i++)
{
if (i % 1000 == 0)
Console.WriteLine("the num is {0}", i.ToString());
}
ev.WaitOne();
//for (int i = 0; i < 10000; i++)
//{
// if (i % 1000 == 0)
// Console.WriteLine("the num is {0}", i.ToString());
//}
Console.WriteLine("{0}:bye...", Thread.CurrentThread.ManagedThreadId);
Console.Read();
}
}
}