【问题标题】:how to call async method until get success response?如何调用异步方法直到获得成功响应?
【发布时间】:2010-12-28 13:39:22
【问题描述】:

我正在通过委托进行异步方法调用。指向函数的委托是一个空函数。我怎么知道异步函数已成功执行,如果没有再次调用该函数,直到我得到成功响应。这是我的代码-

BillService bs = new BillService();
PayAdminCommisionDelegate payCom = new PayAdminCommisionDelegate(bs.PaySiteAdminByOrderNo);
payCom.BeginInvoke(OrderNo,null,null);

【问题讨论】:

    标签: .net asynchronous delegates


    【解决方案1】:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace FewAsyncCalls
    {
        class Program
        {
            static void Main(string[] args)
            {
                var d = new Action(LongErrorProneMethod);
                AsyncCallback callback = null;
                callback = new AsyncCallback(r =>
                    {
                        try
                        {
                            d.EndInvoke(r);
                        }
                        catch
                        {
                            Console.WriteLine("Async call failed");
                            d.BeginInvoke(callback, null);
                        }
                    });
                d.BeginInvoke(callback, null);
                Console.ReadLine();
            }
    
            private static void LongErrorProneMethod()
            {
                Console.WriteLine("Running long error prone method.");
                Thread.Sleep(1000);
                if (new Random().Next(100) > 10)
                    throw new InvalidOperationException();
                else
                    Console.WriteLine("Async call successful");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      相关资源
      最近更新 更多