How to execute the callback method in same thread which calls Asynchronous function.
the caller thread may not be UI Thread… But UI Should not hang..
Thanks & Regards,
Dinesh
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no magic bullet that will allow one thread to initiate the execution of a delegate onto another thread. The target thread must be specially constructed to allow this. In the case of the UI thread there is a message pump that dispatches and processes messages. This message pump can be used to perform the marshaling operation via the
ISynchronizeInvokeinterface.In your case the thread calling the asynchronous function must have some kind of producer-consumer mechanism built into it to get a callback to execute asynchronously on that thread after it has been instructed to do so from the worker thread. Unfortunately, this is not a trivial problem to solve.
Here is one way you can create a thread that can accept delegates to be executed.
It can be used like this.
Update:
Per the comment below
BlockingCollectionis only available in .NET 4.0 or as part of the Reactive Extensions download. If this data structure is not available to you then this already difficult code just became even harder.