Quan Nguyen
Quan Nguyen

Reputation: 582

Need help controlling event handler synchronously in c#

I have a code snippet as below:

public void PopulateItemList()
{
   foreach(var item in collection)
   {
       bool isSuccess = GetSubItem(item.Id);
       if(!isSuccess)
       {
           ShowError();
           break;
       }
   }
}

public bool GetSubItem(string parentId)
{
    bool isSuccess = false;
    _service.Communication<ViewModel, Request, Response>((ViewModel vm, ref Request) => 
   {
       request.ApiName = Const.FXXXName;
       request.Id = parentId;
   }, (viewModel, response, error) =>
   {
       DoSomething();
       ...
       isSuccess = response.IsOk;   // I put a breakpoint here
   }
   return isSuccess;   // I also put a breakpoint here
}

The prototype of Communication function is as below:

public void Communication<VM, S, R>(WebSocketOut<VM, S) send, WebSocketIn<VM, R> recv)

where is WebSocketIn and WebSocketOut are two delegates. The problem is when I debug the program, the "return isSuccess;" statement is always run before it receive the result from "response.IsOk" so the returned value of that func is always "false". In other words, the order of execution is:

  1. return isSuccess
  2. isSuccess = response.IsOk (after MessageReceived(WebSocket4Net) event fired)

I don't know how to make it run in reversed order. Any advice, recommendations or help will be greatly appreciated.

Thanks

Upvotes: 2

Views: 101

Answers (2)

daniel
daniel

Reputation: 1070

Try to use AutoResetEvent to wait async function complete.

public bool GetSubItem(string parentId)
{
    System.Threading.AutoResetEvent autoEvent = new System.Threading.AutoResetEvent(false);

    bool isSuccess = false;

    ThreadPool.QueueUserWorkItem((o) =>
    {
        _service.Communication<ViewModel, Request, Response>((ViewModel vm, ref Request) => 
       {
           request.ApiName = Const.FXXXName;
           request.Id = parentId;
       }, (viewModel, response, error) =>
       {
           DoSomething();
           ...
           isSuccess = response.IsOk;   // I put a breakpoint here
           autoEvent.Set();
       }
   });
   autoEvent.WaitOne();
   return isSuccess;   // I also put a breakpoint here
}

Upvotes: 1

Eser
Eser

Reputation: 12546

You can use async/await and TaskCompletionSource

async public void PopulateItemList() //!!async keyword
{
   foreach(var item in collection)
   {
       bool isSuccess = await GetSubItem(item.Id); //!!await
       if(!isSuccess)
       {
           ShowError();
           break;
       }
   }
}

public Task<bool> GetSubItem(string parentId)
{
    var tcs = new TaskCompletionSource<bool>(); //!!
    _service.Communication<ViewModel, Request, Response>((ViewModel vm, ref Request) => 
   {
       request.ApiName = Const.FXXXName;
       request.Id = parentId;
   }, (viewModel, response, error) =>
   {
       DoSomething();
       ...
       tcs.SetResult(response.IsOk); //!!
   }
   return tcs.Task;
}

Upvotes: 3

Related Questions