Block method until async operation finishes

Hi,

I'm having the following issue and hope someone can help out:
I'm implementing an interface for a 3rd party application. One of the methods should return an array. The problem is that in this method I have to call a method of a COM object that is sitting on an extra form (for events to work) and this one returns the data with several events and signals when it's finished by a flag set in one of the returned objects.
So, somehow I need to find a way to block the method until all the data is returned from the call to the COM object.
Initially I was doing something like:
while(!myCollection.IsFinished)
Thread.Sleep(1);
This works when the class is created on a separate thread but when it's sitting on a WinForm it doesn't.
Any ideas

Thanks,

Tom


Answer this question

Block method until async operation finishes

  • Punch

    thanks, that works. Me stupid had

    while(!myCollection.IsFinished)
    {
    Thread.Sleep(1);
    Application.DoEvents();
    }

    ... before, which of course didn't work.

  • Steve Harding

    It is murky why this would work in a background thread but not in the GUI thread. Other than that your "other form" or the COM component needs the message loop running to get its job done. Start by replacing Thread.Sleep(1) by Application.DoEvents()...


  • Block method until async operation finishes