Click here to Skip to main content
13,087,235 members (59,872 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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()
        public void SiebelMethod()
        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));
        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();
Posted 1-May-13 7:45am
Updated 1-May-13 9:11am
Isn't using threading much more straightforward, simple and reliable?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


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);

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 in that queue if the request is executed then update the value

private IsWorkFinish=bool

void ThreadProc(Object stateInfo)
         IsWorkFinsh =  UpdateGridWithNewData((CallLogHistory)stateInfo);

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 3 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100