0

I am calling a method by using delegates and the target function calls a callback once it is complete. However I do want to wait till its callback is called so that I can get the data.

For example:

//setup 

public IAsyncResult BeginGetTies(Request request, AsyncCallback cb, object asyncState)
{
     _service.GetTiesCompleted += Callback;
    _gets = new getTimeSeriesData(GetTies);

    IAsyncResult r = _gets.BeginInvoke(request, cb, asyncState);

    return r;
}

private void GetTies(Request request)
{
    _service.GetTies(tsr, id);
}

private void Callback(object sender, event e)
{
    // this gets called after EndGet 
}

Now this _service.GetTies uses a callback with events. However I get the control back when _service.GetTies calls gets completed. After that the callback is invoked. I want to wait till the callback gets invoked so that I can use the results.

Please help.

wayfare
  • 1,802
  • 5
  • 20
  • 37
  • What version of .NET are you using? – M.Babcock Nov 14 '13 at 02:30
  • 1
    Interesting. So you want the call back but not the asynchronous aspect? Why not just call the function directly? – LoztInSpace Nov 14 '13 at 02:56
  • I want to use both. The problem is that the EndGet gets called as soon as GetTies is complete, but the data comes in Callback. If I cant resolve this then I will use synchronous model but I do want to find a solution to this issue. – wayfare Nov 14 '13 at 03:01

1 Answers1

0

Using WaitHandles should get you the desired results.

IAsyncResult result1 = Method.BeginInvoke(10, MyCallback, null)
result1.AsyncWaitHandle.WaitOne();  // blocks until finished
Community
  • 1
  • 1
nemec
  • 1,044
  • 9
  • 35