Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Asynchronous
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 1-May-13 8:45am
Edited 1-May-13 10:11am
CPallini326.3K
v3
Comments
Sergey Alexandrovich Kryukov at 1-May-13 13:57pm
   
Isn't using threading much more straightforward, simple and reliable?
—SA

1 solution

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

Solution 1

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);
 
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);
       }
 
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 265
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 3 May 2013
Copyright © CodeProject, 1999-2014
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