终止异步委托 [英] terminating an asynchronous delegate

查看:120
本文介绍了终止异步委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想异步调用两个方法。当其中一个方法完成执行时,另一个方法应该中止。这是正确的方法。任何人都可以建议

I want to call two methods asynchronously.When one of them completes execution ,the other one should abort.Is this the right approach .Can anyone please suggest

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TheWaiime
{
    class Program
    {       
        public delegate void MyDelegate();
        IAsyncResult result1 = null;
        IAsyncResult result2 = null;

        public void EaiMethod()
        {
            Console.WriteLine("Eai1");
        }

        public void SiebelMethod()
        {
            Console.WriteLine("Siebel1");        
        }

        public void CallServices()
        {       
            MyDelegate handler1 = new MyDelegate(EaiMethod);

            AsyncCallback callback1 = new AsyncCallback(Callback_call_eai);

            MyDelegate handler2 = new MyDelegate(SiebelMethod);

            AsyncCallback callback2 = new AsyncCallback(Callback_call_siebel);

            result1 = handler1.BeginInvoke(callback1, null);     

             result2 = handler2.BeginInvoke(callback2, null);
            
            result1.AsyncWaitHandle.WaitOne(new TimeSpan(0,0,2));
            result2.AsyncWaitHandle.WaitOne(new TimeSpan(0,0,2));
            
            result1.AsyncWaitHandle.Close();
            result2.AsyncWaitHandle.Close();  
        }

        public void Callback_call_eai(IAsyncResult ar)
        {
            if (!result2.IsCompleted)
            {
                result2.AsyncWaitHandle.WaitOne(new TimeSpan(0,0,0));    
            }         
        }

        public void Callback_call_siebel(IAsyncResult ar)
        {
            if (!result1.IsCompleted)
            {
                result1.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, 0));              
            }
        }

        static void Main(string[] args)
        {
            Program pg = new Program();
            pg.CallServices();
        }
    }
}

推荐答案






您发现解决方案的方式并非优化。如你所知,如果你使用了Thread,那么在增加调用的情况下它会妨碍性能。我有类似的问题。我做的是我用过



Hi,

the way you have found the solution is not as optimize. as you know if you used the Thread then in case of increasing calls it will hamper the performance. I have similar problem. What i have done is I used

ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ActivityInfo);





帮助我排队请求。根据可用线程对请求进行排队后,如果执行请求,它将从该队列中的queue.now将操作传递给函数execut,然后更新值



private IsWorkFinish = bool





that helped me to que the request. Once you que the request as per the available thread it will pass your operation to the function execut from the queue.now in that queue if the request is executed then update the value

private IsWorkFinish=bool

void ThreadProc(Object stateInfo)
       {
         if(!IsWorkFinish)
         IsWorkFinsh =  UpdateGridWithNewData((CallLogHistory)stateInfo);
       }





现在创建一个异步方法并将此值传递给异步方法。在您的Async end方法中,您可以更新isWorkFinish。

我建议使用.Net Fremworks ThreadPool,因为它已经过优化,您无需处理任何事情。



now create one Async Method and pass this value to async method. in your Async end method you can update isWorkFinish.
I have suggested the .Net Fremworks ThreadPool because it is optimized and you no need to handle any thing.


这篇关于终止异步委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆