我最近找到了这样的一段代码,他想演示如何解决异步操作的线程同步问题。
具体操作是这样的:主线程异步调用后不断地轮询判断异步调用返回的IAsyncResult的IsCompleted(标示是否异步调用完成)属性,如果IsCompleted为真就不再轮询,进行后续的操作。
我在运行代码时发现了问题:异步操作没有完成的时候,IsCompleted的值为true了。然后我又从MSDN上面复制一段代码调试,发现MSDN的运行结果正常,只有在异步操作完成后IsCompleted的值才由false变为true。
我个人认为这两段代码的区别就是MSDN异步调用自定义委托的BeginInvoke,另外一段代码是异步调用System.IO.FileStream类的BeginRead
//这是MSDN的代码:
using System;
using System.Threading;
namespace ConsoleApplication1
{
public class AsyncDemo
{
// The method to be executed asynchronously.
public string TestMethod(int callDuration, out int threadId)
{
Console.WriteLine("Test method begins.");
Thread.Sleep(callDuration);
threadId = Thread.CurrentThread.ManagedThreadId;
return String.Format("My call time was {0}.", callDuration.ToString());
}
}
// The delegate must have the same signature as the method
// it will call asynchronously.
public delegate string AsyncMethodCaller(int callDuration, out int threadId);
class Program
{
static void Main(string[] args)
{
// The asynchronous method puts the thread id here.
int threadId;
// Create an instance of the test class.
AsyncDemo ad = new AsyncDemo();
// Create the delegate.
AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
// Initiate the asychronous call.
IAsyncResult result = caller.BeginInvoke(10000,
out threadId, null, null);
// Poll while simulating work.
while (result.IsCompleted == false)
{
Thread.Sleep(250);