I am working with TPL and need to have a long running TPL task send results to the parent UI thread without terminating. I have tried several approaches and have been googling quite a bit. Does anyone know how to make this happen with TPL?
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
- Should I use static function in c# where many call
You could pass in a delegate to call with periodic results, and a
SynchronizationContext
which the task could use to invoke the callback on the correct thread. That's basically the way thatBackgroundWorker
does it (and the way that the async feature of C# 5 will "know" where to call you back) - it capturesSynchronizationContext.Current
on the calling thread, then callsPost
(IIRC) to post a message to the right context. You then just need to wrap the original callback in aSendOrPostCallback
which executes it when it's got to the right thread.EDIT: Sample program:
Depending on the application you are using there might be different approaches.