Asynchronous call synchronization

I am trying to synchronize an asynchronous call.

A regular ( async ) thread is as follows:

  • Server request to use data via telnet: 'Session.sendToTarget (message)'
  • The application moves on other things ....
  • When the server response is ready, the server will send the result.
  • Application receives result and event "OnDataReceived"

Data from the server is critical for the next step, so I want to save ALL until it works.

The sync stream should look like this:

  • Server request for data: Session.sendToTarget (message)
  • Wait for the data received from the server

Using C #, I tried to synchronize the operation with "WaitHandle.WaitOne (TimeToWaitForCallback)" unsuccessfully. It looks like WaitOne is stopping the application to receive incoming messages (I also tried to wait in another thred). Afther TimeToWaitForCallback time pass I receive an incoming message that stops deu for the WaitOne action.

my attempt to do code synchronization:

public virtual TReturn Execute(string message) { WaitHandle = new ManualResetEvent(false); var action = new Action(() => { BeginOpertaion(message); WaitHandle.WaitOne(TimeToWaitForCallback); if (!IsOpertaionDone) OnOpertaionTimeout(); }); action.DynamicInvoke(null); return ReturnValue; } 

Inbox raises this code:

 protecte protected void EndOperation(TReturn returnValue) { ReturnValue = returnValue; IsOpertaionDone = true; WaitHandle.Set(); } 

Any ideas?

+4
source share
3 answers
  AutoResetEvent mutex = new AutoResetEvent(false); ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { Thread.Sleep(2000); Console.WriteLine("sleep over"); mutex.Set(); })); mutex.WaitOne(); Console.WriteLine("done"); Console.ReadKey(); 

put mutex.Set () in the event handler when the async completion completes ...

ps: I like streaming recording: P

+2
source
0
source

Below lines

A regular (asynchronous) stream looks like this:

  Asking the server for data using telnet: 'Session.sendToTarget(message)' The app move on doing other things.... When the server answer ready, the server send the result. The app get the result and raise event "OnDataReceived" 

and moving on to the next

 Asking the server for data: Session.sendToTarget(message) Wait until the data received from the server 

it is as good as a lock request, so just call Session.sendToTarget (message) synchronously. No use making asynchronous

0
source

All Articles