1

考虑这段代码:

class Program
        {
            static void Main(string[] args)
            {
                Master master = new Master();
                master.Execute();
            }
        }

    class TestClass
    {
        public void Method(string s)
        {
            Console.WriteLine(s);
            Thread.Sleep(5000);
            Console.WriteLine("End Method()");
        }
    }
    class Master
    {
        private readonly TestClass test = new TestClass();

        public void Execute()
        {
            Console.WriteLine("Start main thread..");
            Action<String> act = test.Method;
            IAsyncResult res = act.BeginInvoke("Start Method()..", x =>
            {
                Console.WriteLine("Start Callback..");
                act.EndInvoke(x);
                Console.WriteLine("End Callback");
            }, null);
            Console.WriteLine("End main thread");
            Console.ReadLine();
        }
    }

我们有结果:

Start main thread..
End main thread
Start Method()..
End Method()
Start Callback..
End Callback

所以,我想要结果:

Start main thread..    
Start Method()..
End Method()
Start Callback..
End Callback
End main thread

我怎么能async在这段代码中等待?我查看了 MSDN 文章“异步调用同步方法”,发现:

调用后,BeginInvoke您可以执行以下操作:

  • 做一些工作,然后调用EndInvoke阻塞直到调用完成。
  • WaitHandle使用该IAsyncResultAsyncWaitHandle
    属性获取 a ,使用其WaitOne方法阻止执行,直到
    WaitHandle发出信号,然后调用EndInvoke.
  • 轮询 IAsyncResult返回的 byBeginInvoke以确定异步调用何时完成,然后调用EndInvoke.
  • 将回调方法的委托传递给BeginInvoke. 当异步调用完成时,该方法在ThreadPool线程上执行。回调方法调用EndInvoke.

我认为这一秒对我来说更好。但是如何实现呢?特别是我对重载感兴趣WaitOne()阻塞当前线程,直到当前 WaitHandle 收到信号)。如何正确地做到这一点?我的意思是这种情况下的常见模式。

更新:

现在我使用Task<T>

 class Program
    {
        static void Main(string[] args)
        {
            Master master = new Master();
            master.Execute();
        }
    }

    class WebService
    {
        public int GetResponse(int i)
        {
            Random rand = new Random();
            i = i + rand.Next();
            Console.WriteLine("Start GetResponse()");
            Thread.Sleep(3000);
            Console.WriteLine("End GetResponse()");
            return i;
        }

        public void SomeMethod(List<int> list)
        {
            //Some work with list        
            Console.WriteLine("List.Count = {0}", list.Count);
        }
    }
    class Master
    {
        private readonly WebService webService = new WebService();

        public void Execute()
        {
            Console.WriteLine("Start main thread..");
            List<int> listResponse = new List<int>();
            for (int i = 0; i < 5; i++)
            {
                var task = Task<int>.Factory.StartNew(() => webService.GetResponse(1))
                    .ContinueWith(x =>
                    {
                        Console.WriteLine("Start Callback..");
                        listResponse.Add(x.Result);
                        Console.WriteLine("End Callback");
                    });
            }
            webService.SomeMethod(listResponse);
            Console.WriteLine("End main thread..");
            Console.ReadLine();
        }
    }

SomeMethod()这是空的主要问题list

结果: 在此处输入图像描述

现在我有可怕的解决方案:(

 public void Execute()
        {
            Console.WriteLine("Start main thread..");
            List<int> listResponse = new List<int>();
            int count = 0;
            for (int i = 0; i < 5; i++)
            {
                var task = Task<int>.Factory.StartNew(() => webService.GetResponse(1))
                    .ContinueWith(x =>
                    {
                        Console.WriteLine("Start Callback..");
                        listResponse.Add(x.Result);
                        Console.WriteLine("End Callback");
                        count++;
                        if (count == 5)
                        {
                            webService.SomeMethod(listResponse);
                        }
                    });

            }
            Console.WriteLine("End main thread..");
            Console.ReadLine();
        }

结果:

在此处输入图像描述

这就是我需要等待异步调用的原因。我如何WaitTask这里使用?

更新 2:

class Master
{
    private readonly WebService webService = new WebService();
    public delegate int GetResponseDelegate(int i);

    public void Execute()
    {
        Console.WriteLine("Start main thread..");
        GetResponseDelegate act = webService.GetResponse;
        List<int> listRequests = new List<int>();
        for (int i = 0; i < 5; i++)
        {
            act.BeginInvoke(1, (result =>
            {                    
                int req = act.EndInvoke(result);
                listRequests.Add(req);
            }), null);  
        }

        webService.SomeMethod(listRequests);
        Console.WriteLine("End main thread..");
        Console.ReadLine();
    }
}
4

2 回答 2

1

从您的示例代码中不清楚为什么需要异步调用。如果你不这样做,你可以只test.Method();同步调用。

假设您需要异步执行,请不要为过时的Delegate.BeginInvoke东西烦恼。使用Task基于新的 API:

var task = Task.Factory.StartNew(() => test.Method());

然后,您可以Wait在任务上或await它或使用Task.ContinueWith(为您的情况选择适当的技术)。

于 2013-01-13T14:53:52.050 回答
1

我认为这就是你想要的。我们正在启动任务,然后等待它们完成,所有完成后,我们正在获得结果。

class Program
{
    static void Main(string[] args)
    {
        Master master = new Master();
        master.Execute();
    }
}

class WebService
{
    public int GetResponse(int i)
    {
        Random rand = new Random();
        i = i + rand.Next();
        Console.WriteLine("Start GetResponse()");
        Thread.Sleep(3000);
        Console.WriteLine("End GetResponse()");
        return i;
    }

    public void SomeMethod(List<int> list)
    {
        //Some work with list        
        Console.WriteLine("List.Count = {0}", list.Count);
    }
}
class Master
{
    private readonly WebService webService = new WebService();
    public void Execute()
    {
        Console.WriteLine("Start main thread..");
        var taskList = new List<Task<int>>();
        for (int i = 0; i < 5; i++)
        {
            Task<int> task = Task.Factory.StartNew(() => webService.GetResponse(1));
            taskList.Add(task);
        }

        Task<List<int>> continueWhenAll =
            Task.Factory.ContinueWhenAll(taskList.ToArray(),
                            tasks => tasks.Select(task => task.Result).ToList());

        webService.SomeMethod(continueWhenAll.Result);
        Console.WriteLine("End main thread..");
        Console.ReadLine();
    }

}

BeginIvoke/EndInvoke 的丑陋解决方案

public void Execute()
{
    Func<int, int> func = webService.GetResponce;

    var countdownEvent = new CountdownEvent(5);
    var res = new List<int>();
    for (int i = 0; i < 5; ++i)
    {
        func.BeginInvoke(1, ar =>
                        {
                            var asyncDelegate = (Func<int, int>)((AsyncResult)ar).AsyncDelegate;
                            int ii = asyncDelegate.EndInvoke(ar);
                            res.Add(ii);
                            ((CountdownEvent)((AsyncResult)ar).AsyncState).Signal();
                        }, countdownEvent);
    }
    countdownEvent.Wait();
    Console.WriteLine(res.Count);
}
于 2013-01-13T19:05:10.087 回答