开发者

how to call async method until get success response?

开发者 https://www.devze.com 2023-02-01 17:47 出处:网络
开发者_运维百科I am making a async method call through a delegate. Delegate pointing to a function is a void function. How can I know that the async function has been executed successfully and if not
开发者_运维百科

I am making a async method call through a delegate. Delegate pointing to a function is a void function. How can I know that the async function has been executed successfully and if not the again call that function untill I get success response. here my code-

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


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");
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消