Silverlight, dealing with Async calls

后端 未结 8 1271
走了就别回头了
走了就别回头了 2021-01-03 01:23

I have some code as below:

        foreach (var position in mAllPositions)
        {
                 DoAsyncCall(position);
        }
//I want to execute co         


        
8条回答
  •  自闭症患者
    2021-01-03 01:52

    (NB, I am giving you two separate answers. This one sticks as close to your question as possible.)

    Your question says

    while{ count >= mAllPositions.Count )
    {
        //Run my code here
    }
    

    but I am guessing that what you really mean is:

    while( count < mAllPositions.Count )
       ; // do nothing -- busy wait until the count has been incremented enough
    // Now run my code here
    

    ??

    If so, then you can accomplish the same thing more efficiently by using a semaphore:

    Semaphore TheSemaphore = new Semaphore( 0, mAllPositions.Count );
    

    As each async call completes, release the semaphore.

    TheSemaphore.Release();
    

    Before executing your final code, ensure the semaphore has been released the requisite number of times:

     for( int i=0; i

    Your code will block until the async operations have all been completed.

提交回复
热议问题