Let's say that I have a task called a tasking (which I can not modify) which is a method "DoTask" TaskCompleted, which gives exposure to some potentially long calculations and results through an event. Generally it is called in the windows window, which results in the closure of the user's end.
In my special scenario, I need to add some data (a database record) to data returned in efficiency, update database records.
I've checked the use of AutoResetEvent to notify when the event is managed. AutoResetEvent.WaitOne () with that problem will be blocked and the event handler will never be called. Generally, AutoResetAvent is called a separate thread, so I think that the event handler calls on the same thread that it calls.
Essentially I want to turn on an asynchronous call, where the result comes back to a synchronous call through the event (i.e. call DoSyncTask from any other class) until the event is controlled Is not done and the result is accessible for both event handler and method which is called the method to start the async call.
Public class SyncTask {TaskCompletedEventArgs data; AutoResetEvent Task Don; Public SyncTask () {taskDone = new AutoResetEvent (wrong); } Public string DoSyncTask (int latitude, longitude) {task t = new work (); T.Completed = new TaskCompletedEventHandler (functional); T.DoTask (latitude, longitude); TaskDone.WaitOne (); // app like anything else DoEvents (); TaskDone.Reset () in WinForms; Return data road; } Private Zero Work Completed (Object Sender, Efficiency EventArgs e) {data = e; TaskDone.Set (); // To doSyncTask, there are some other mechanisms to indicate that the work is completed. }} In a Windows app, the following works are correctly I Public Class SyncTask {TaskCompletedEventArgs data; Public SyncTask () {taskDone = new AutoResetEvent (wrong); } Public string DoSyncTask (int latitude, longitude) {task t = new work (); T.Completed = new TaskCompletedEventHandler (functional); T.DoTask (latitude, longitude); While (data == blank) application Duants (); Return data road; } Private Zero Work Completed (Object Sender, Efficiency EventArgs e) {data = e; }}
I need to repeat the behavior in window service, where the application. Run is not called and the ApplicationContext object is not available.
Some of the recent problems with me making strange calls and events on the thread and returning to the main thread was.
I used to keep track of things. The bottom (pseudo) code shows what is currently working for me.
Synchronization contact context; Zero start () {// First reference in the current context // call it back to reference = SynchronizationContext.Current; // Start a thread and call it as // async method, for example: Proxy.BeginCodeLookup (aVariable, New AsyncCallback (LookupResult), AsyncState); // Now what you were doing was continue / and finish the lookup} Zero LookupResult (IAsyncResult result) {// When the async function is finished / this method is called / it is same as the caller on // Thread is, // in this case BeginCodeLookup. Result.AsyncWaitHandle.WaitOne (); Var lookup = Proxy. Endcode lookup (result); // SynchronizationContext.Send method calls the // // reference thread, in this case the main thread reference. (New SendOrPostCallback (OnLookupCompleted), results. AsyncState); } Zero OnLookupCompleted (object state) {// Now this code will be executed on // main thread}
I hope this helps, because it has fixed the problem for me is.
Comments
Post a Comment