views:

68

answers:

3

Since .NET 4.0 there is the TPL to execute asynchronous tasks. If you are reading the msdn, all async operations interacting with forms / UI still uses the InvokeRequire ... Invoke() pattern. What I'm asking is that is there a reason for it? From what I learned, TPL should be sort of a replacement of older threading mechanisms. So what's the point to ignore it when it's about UI threading? Any thoughts about that?

+3  A: 

This seems fairly subjective...

When you say "Since .NET 4.0", you are saying "as of April of this year" - .net has been around for 10 years now, and InvokeRequired/Invoke has been used for the last 9. Why would MS break all existing UI code for any reason? Even if a new way of calling to a thread existed, they could not simply modify the pattern without huge compatibility concerns.

Also, the TPL is not analogous to InvokeRequired/Invoke - the TPL is about easy parallelism, and invoke is about running code on a specific thread. I'm not sure why one would replace the other even if there were no compatibility concerns.

Note that there is nothing stopping you from using the TPL to ensure you call UI components on the correct thread. In fact, you can easily do this. But that is up to you and the current API is not going to change in a way that isn't backwards compatible.

Philip Rieck
+1  A: 

With TPL you can specify target thread using TaskScheduler.FromCurrentSynchronizationContext this method specify that Task will be executed on main thread. I recommend using it instead of Invoke.

Andrey
+1  A: 

What is the question here? The existence of TPL doesn't change the fact that UIs are inherently single-threaded, and require controls to be accessed only on the UI thread. (And that's a Windows limitation, not a limitation of the .NET UI frameworks. TPL can't change decades of Windows design limitations.)

If your question is about mixing Tasks with InvokeRequired/Invoke, there is a more TPL-oriented way than Invoke. TPL comes with a built-in way to schedule a continuation Task to run on the UI thread. So you put your background-oriented work in one Task, and then your UI updates in another Task. See this post on task schedulers and SynchronizationContext.

(But really, TPL doesn't replace any of the older thread APIs. If Invoke is the best way to do what you're trying to do, use it.)

Joe White