tags:

views:

150

answers:

7

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.
}
+3  A: 

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
A: 

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
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
Tommy
A: 

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

Tommy
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
@James Kolpack: yes, but this is a literal answer to the OPS question
Tommy
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
@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
Tommy
@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
@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'
Tommy
A: 

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
A: 

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
+1  A: 

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