2

I have 2 functions that needs to be executed one after the other. In this function, async calls are made. How do I go about executing the second function after the async call is completed?

For eg.

public void main()
{
   executeFn("1");
   executeFn("2"); //I want this to be executed after 1 has finished.
}


private bool executeFn(string someval)
{
     runSomeAsyncCode(); //This is some async uploading function that is yet to be defined.
}
GatesReign
  • 826
  • 1
  • 9
  • 23

6 Answers6

3

You can use Thread.Join.

But then I do not see the point of async execution of those 2 functions as they become sequential.

Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130
1

Let runSomeAsyncCode() return an IAsyncResult and implement the BeginX EndX methods similar to the CLR Asynchronous Programming Model. Use the EndX method to wait for the code to finish executing.

Cornelius
  • 3,526
  • 7
  • 37
  • 49
0

Your async method you're calling must have something to notify the caller when it's completed am I correct? (otherwise it would be just execute and forget, which is unlikely) If so, you simply have to wait for the notification to come up and execute the second method.

Jaya Wijaya
  • 179
  • 4
  • i assume that he is looking for a way to send many executions of this operation, and that they could be created dynamically. And if they are, to queue them in wait for the current operation to finnish – caesay Mar 10 '10 at 20:44
0

try this:

public void main() 
{ 
     executeFn("1"); 
     executeFn("2");
} 
List<string> QueuedCalls = new List<string>(); // contains the queued items
bool isRunning = false; // indicates if there is an async operation running
private bool executeFn(string someval) 
{ 
    if(isRunning) { QueuedCalls.Add(someval); return; } // if there is an operation running, queue the call
    else { isRunning = true; } // if there is not an operation running, then update the isRunning property and run the code
    runSomeAsyncCode(); //undefined async operation here<- 
    isRunning = false; //get here when the async is completed, (updates the app telling it this operation is done)
    if(QueuedCalls.Count != 0)//check if there is anything in the queue
    {
        //there is something in the queue, so remove it from the queue and execute it.
        string val = QueuedCalls[0];
        QueuedCalls.RemoveAt(0);
        executeFn(val);
    }
} 

this way will not block any threads, and will simply execute the queued call when the first finnishs,which is what i believe you want! happy coding! now id recommend running the last section, at where it sets the isRunning to false, inside your async operation, or trigger it with an event or something, the only catch is that peice of code has to be executed when your async operation is completed, so however you want to do that is up to you

caesay
  • 16,932
  • 15
  • 95
  • 160
  • The .NET framework provides many options for thread management, it's easier to use what's built in. I'd recommend looking at a BackgroundWorker instead of this - http://www.albahari.com/threading/part3.aspx#_BackgroundWorker – James Kolpack Mar 10 '10 at 02:01
  • @James Kolpack: yes, but this is a literal answer to the OPS question – caesay Mar 10 '10 at 02:03
  • won't this code just execute both functions immediately? assuming runSomeAsyncCode() returns immediately then isRunning is set to false straight after, the second call then does exactly the same thing – Matt Mar 10 '10 at 05:13
  • @Matt No.. because as i commented, you would include the isRunning and check code in your async operation, or put it in an event signalling that the async is done, CLEARLY states this in my documentation – caesay Mar 10 '10 at 20:34
  • @Tommy, lol, yes the 'so however you want to do that is up to you' bit of your comment is what the OP is asking... and the code you give is misleading, imo – Matt Mar 10 '10 at 22:05
  • @Matt: but i did say right before that, 'the only catch is that peice of code has to be executed when your async operation is completed' – caesay Mar 12 '10 at 16:35
0

You can consider using Generic delegates execute the first method async then in the call back execute the other method async. If you are really worried executing them sync with respect to each other.

StevenzNPaul
  • 188
  • 12
0

One simple way is to use a custom threadpool

http://www.codeplex.com/smartthreadpool

You can instantiate a separate threadpool, Set the threadpool size to 1, and queue the workers

Timur Fanshteyn
  • 2,266
  • 2
  • 23
  • 27