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?
source share