Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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()
        {
            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();
        }
    }
}
Posted
Updated 1-May-13 9:11am
v3
Comments
Sergey Alexandrovich Kryukov 1-May-13 13:57pm    
Isn't using threading much more straightforward, simple and reliable?
—SA

1 solution

C#

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

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

private IsWorkFinish=bool

C#
void ThreadProc(Object stateInfo)
       {
         if(!IsWorkFinish)
         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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900