C# Winform异步调用详解

C#异步调用四大方法是什么呢?C#异步调用四大方法的使用是如何进行的呢?让我们首先了解下什么时候用到C#异步调用:

.NET Framework 允许您C#异步调用任何方法。定义与您需要调用的方法具有相同签名的委托;公共语言运行库将自动为该委托定义具有适当签名的 BeginInvoke 和 EndInvoke 方法。

BeginInvoke 方法用于启动C#异步调用。它与您需要异步执行的方法具有相同的参数,只不过还有两个额外的参数(将在稍后描述)。BeginInvoke 立即返回,不等待C#异步调用完成。BeginInvoke 返回 IasyncResult,可用于监视调用进度。

EndInvoke 方法用于检索C#异步调用结果。调用 BeginInvoke 后可随时调用 EndInvoke 方法;如果C#异步调用未完成,EndInvoke 将一直阻塞到C#异步调用完成。EndInvoke 的参数包括您需要异步执行的方法的 out 和 ref 参数(在 Visual Basic 中为 ByRef 和 ByRef)以及由 BeginInvoke 返回的 IAsyncResult。

注意:Visual Studio .NET 中的智能感知功能会显示 BeginInvoke 和 EndInvoke 的参数。如果您没有使用 Visual Studio 或类似的工具,或者您使用的是 C# 和 Visual Studio .NET,请参见异步方法签名获取有关运行库为这些方法定义的参数的描述。

本主题中的代码演示了四种使用 BeginInvoke 和 EndInvoke 进行C#异步调用的常用方法。调用了 BeginInvoke 后,可以:

· 进行某些操作,然后调用 EndInvoke 一直阻塞到调用完成。

· 使用 IAsyncResult.AsyncWaitHandle 获取 WaitHandle,使用它的 WaitOne 方法将执行一直阻塞到发出 WaitHandle 信号,然后调用 EndInvoke。

· 轮询由 BeginInvoke 返回的 IAsyncResult,确定C#异步调用何时完成,然后调用 EndInvoke。

· 将用于回调方法的委托传递给 BeginInvoke。该方法在C#异步调用完成后在 ThreadPool 线程上执行,它可以调用 EndInvoke。

警告:始终在C#异步调用完成后调用 EndInvoke。

测试方法和异步委托

四个示例全部使用同一个长期运行的测试方法 TestMethod。该方法显示一个表明它已开始处理的控制台信息,休眠几秒钟,然后结束。TestMethod 有一个 out 参数(在 Visual Basic 中为 ByRef),它演示了如何将这些参数添加到 BeginInvoke 和 EndInvoke 的签名中。您可以用类似的方式处理 ref 参数(在 Visual Basic 中为 ByRef)。

下面的代码示例显示 TestMethod 以及代表 TestMethod 的委托;若要使用任一示例,请将示例代码追加到这段代码中。

注意  为了简化这些示例,TestMethod 在独立于 Main() 的类中声明。或者,TestMethod 可以是包含 Main() 的同一类中的 static 方法(在 Visual Basic 中为 Shared)。

intoutint Console.WriteLine( Thread.Sleep(callDuration);   threadId = AppDomain.GetCurrentThreadId();   return+ callDuration.ToString();   // The delegate must have the same signature as the method publicdelegatestring intoutint intoutint Console.WriteLine( Thread.Sleep(callDuration);   threadId = AppDomain.GetCurrentThreadId();   return+ callDuration.ToString();   // The delegate must have the same signature as the method publicdelegatestring intoutint

C#异步调用四大方法之使用 EndInvoke 等待异步调用

异步执行方法的最简单方式是以 BeginInvoke 开始,对主线程执行一些操作,然后调用 EndInvoke。EndInvoke 直到C#异步调用完成后才返回。这种技术非常适合文件或网络操作,但是由于它阻塞 EndInvoke,所以不要从用户界面的服务线程中使用它。

// The asynchronous method puts the thread id here. new newAsyncDelegate(ad.TestMethod);   IAsyncResult ar = dlgt.BeginInvoke(3000,    Console.WriteLine( AppDomain.GetCurrentThreadId());   stringout Console.WriteLine("The call executed on thread {0},    return"{1}\"."

C#异步调用四大方法之使用 WaitHandle 等待异步调用

等待 WaitHandle 是一项常用的线程同步技术。您可以使用由 BeginInvoke 返回的 IAsyncResult 的 AsyncWaitHandle 属性来获取 WaitHandle。C#异步调用完成时会发出 WaitHandle 信号,而您可以通过调用它的 WaitOne 等待它。

如果您使用 WaitHandle,则在C#异步调用完成之后,但在通过调用 EndInvoke 检索结果之前,可以执行其他处理。

// The asynchronous method puts the thread id here. new newAsyncDelegate(ad.TestMethod);   IAsyncResult ar = dlgt.BeginInvoke(3000,    Console.WriteLine( AppDomain.GetCurrentThreadId());   // Wait for the WaitHandle to become signaled. ar.AsyncWaitHandle.WaitOne();   stringout Console.WriteLine("The call executed on thread {0},    return"{1}\"."

C#异步调用四大方法之轮询异步调用完成

您可以使用由 BeginInvoke 返回的 IAsyncResult 的 IsCompleted 属性来发现C#异步调用何时完成。从用户界面的服务线程中进行C#异步调用时可以执行此操作。轮询完成允许用户界面线程继续处理用户输入。

// The asynchronous method puts the thread id here. new newAsyncDelegate(ad.TestMethod);   IAsyncResult ar = dlgt.BeginInvoke(3000,    whilefalse stringout Console.WriteLine("The call executed on thread {0},   return"{1}\"."

0x04:C#异步调用四大方法之异步调用完成时执行回调方法

如果启动异步调用的线程不需要处理调用结果,则可以在调用完成时执行回调方法。回调方法在 ThreadPool 线程上执行。

要使用回调方法,必须将代表该方法的 AsyncCallback 委托传递给 BeginInvoke。也可以传递包含回调方法将要使用的信息的对象。例如,可以传递启动调用时曾使用的委托,以便回调方法能够调用 EndInvoke。

// Asynchronous method puts the thread id here. new newAsyncDelegate(ad.TestMethod);   // Initiate the asychronous call. Include an AsyncCallback // delegate reDIVsenting the callback method, and the data IAsyncResult ar = dlgt.BeginInvoke(3000,   newAsyncCallback(CallbackMethod),   Console.WriteLine( // Callback method must have the same signature as the staticvoidCallbackMethod(IAsyncResult ar) {   AsyncDelegate dlgt = (AsyncDelegate) ar.AsyncState;   stringout Console.WriteLine("The call executed on thread {0},   return"{1}\"."

下面看有回调函数的WebRequest和WebResponse的异步操作。

usingusingusingusingusingpublicclassconstint1024publicpublicbytepublicpublicpublicDecoder StreamDecode = Encoding.UTF8.GetDecoder();

publicnewbyte[BUFFER_SIZE];
RequestData = new""nullnullclasspublicstaticnewfalseconstint1024publicstaticvoidstringif1returnnew0HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(HttpSite);

newIAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(newAsyncCallback(RespCallback), rs);

将设置为,以便在调用回调前,应用程序不退出
publicstaticvoid""\r\n"privatestaticvoidRespCallback(IAsyncResult ar)
RequestState rs = (RequestState)ar.AsyncState;

从获取HttpWebRequest req = rs.Request;

调用生成对象该对象来自上面发出的请求
HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(ar);

Stream ResponseStream = resp.GetResponseStream();

该读取操作也使用异步完成,所以我们将要以存储流
rs.ResponseStream = ResponseStream;

请注意,被传入到。这是数据将被读入的位置。
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead, 0newAsyncCallback(ReadCallBack), rs);
}


privatestaticvoidReadCallBack(IAsyncResult asyncResult)
从获取对象
RequestState rs = (RequestState)asyncResult.AsyncState;

取出在中设置的Stream responseStream = rs.ResponseStream;

此时中应该有一些数据。读取操作将告诉我们那里是否有数据
intread = responseStream.EndRead(asyncResult);

if0new将字节流转换为数组,然后转换为字符串显示多少字符被转换为intlen = rs.StreamDecode.GetChars(rs.BufferRead, 00new0将最近读取的数据追加到对象中,该对象包含在中
现在发出另一个异步调用,读取更多的数据请注意,将不断调用此过程,直到返回IAsyncResult ar = responseStream.BeginRead(rs.BufferRead, 0newAsyncCallback(ReadCallBack), rs);
}
elseif(rs.RequestData.Length > 1stringstrContent;
strContent = rs.RequestData.ToString();
Console.WriteLine(strContent);
}

return

在这里有回调函数,且异步回调中又有异步操作。

首先是异步获得ResponseStream,然后异步读取数据。

这个程序非常经典。从中可以学到很多东西的。我们来共同探讨。

总结

在.net framework 类库中也有很多异步调用的方法。一般都是已Begin开头End结尾构成一对,异步委托方法,外加两个回调函数和AsyncState参数,组成异步操作的宏观体现。所以要做异步编程,不要忘了委托delegate、Begin,End,AsyncCallBack委托,AsyncState实例(在回调函数中通过IAsyncResult.AsyncState来强制转换),IAsycResult(监控异步),就足以理解异步真谛了

资源来自网络,分别为:

http://www.cnblogs.com/ericwen/archive/2008/03/12/1101801.html

http://developer.51cto.com/art/200908/145541.htm


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值