异步调用,回调方法,向回调方法传递状态
来源:互联网 发布:sql2000附加数据库失败 编辑:程序博客网 时间:2024/05/01 13:51
/* 异步调用; 线程在调用方法后立即返回而不去等待调用完成,尤其是在以异步方式管理数据流或在管理对处在另一个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();
}
}
}
- 异步调用,回调方法,向回调方法传递状态
- RMI异步回调方法调用
- jQuery之异步Ajax请求使用 通过传递参数来调用后台方法返回状态
- c#异步回调方法
- jQuery中on方法向回调函数传递参数
- 实现android异步调用WEB API的方法,并实现回调
- ajax 异步调用方法
- C#异步方法调用
- 异步调用服务器端方法
- C#异步方法调用
- 串口异步调用方法
- 如何异步调用方法
- 异步调用方法
- C# 异步方法调用
- php 异步调用方法
- php 异步调用方法
- Spring 异步方法调用
- java 调用异步方法
- 将SQLServer2005 数据库还原或恢复到SQLServer2000数据库中
- 程序员四大忌
- 隐式挖掘网站用户行为
- Redhat Enterprise Linux AS 4 安装流程
- 有关重复记录的删除(SQL SERVER)(不是很好的方法,可以参考一下,有待更新)
- 异步调用,回调方法,向回调方法传递状态
- 上传
- 我也玩 PSP 开发! —— PSP开发环境 简介
- 程序集强命名全解析
- 一个程序菜鸟的年终总结
- 正则表达式基础
- 如何使Microsoft SQL Server的日志文件不会增大
- The Baby Eagle 小鹰的故事
- Eclipse的快捷键一览