Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am pretty much new to WPF. I want an example for multiple tasks. Basically I have a start button on the mainview, click it then there are several long tasks concurrently running, but the the maximum number of tasks are limited.

I tried some exercise say letting two tasks running, however I only found one task run and the other was blocked. I am not sure if WPF blocks it.

I haven't found such an example online. I feel it is hard to handle multiple thread in WPF.
BTW, what is the best resource to learn WPF?
Posted

zhshqzyc wrote:
@SA, My understanding for Invoke or BeginInvoke is only for UI? Can we use them for long running back end process?
You are right. If you want to delegate some execution to some other thread, this is a totally different issue — you have to implement such behavior yourself.

My article, complete with full source code and usage examples, explains it all: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

With .NET v.4.0 and later, there is a faster Microsoft implementation of the collection (http://msdn.microsoft.com/en-us/library/dd267312%28v=vs.110%29.aspx[^]), but, in contrast to MSDN documentation, by articles explains the internals and provides more advanced use cases and explanations of the usage.

—SA
 
Share this answer
 
Comments
[no name] 24-Sep-14 9:36am    
Hi I wanted to vote the answer. However the system asked me again and again You must have a confirmed email address to vote. I did confirmed the email. I think that it is the bug of this web site. I am very sorry for being unable to vote your answer.
Sergey Alexandrovich Kryukov 24-Sep-14 10:19am    
Thank you for the accepting and the attempt to vote.

Probably you will be able to do it later. I don't really know why your mail is considered unconfirmed, maybe you confirmed it recently; and some more time is needed for next step. This is the question you can ask here: Bugs and suggestions (even if this is not a bug and not a suggestion, just a concern). Most problems reported are addressed, often in timely manner.

By the way, are you going to accept Solution 1, too? In all cases, your follow-up questions will be welcome; and other members can continue to add answers.

Thank you.
—SA
Learn: threading, Dispatcher.Invoke, Dispatcher.BeginInvoke.
Now, you know the keywords to search for. First, do some search and read on the topics. While reading, you will find thousands of code samples, if not millions.

One of the place to look for is CodeProject articles and answers:
http://www.codeproject.com/search.aspx?q=WPF+%28Invoke+OR+BeginInvoke%29&doctypeid=1[^],
http://www.codeproject.com/search.aspx?q=WPF+%28Invoke+OR+BeginInvoke%29&doctypeid=5[^].

This way, you will get much less search results, but the relevance will be nearly guaranteed for most of them.

—SA
 
Share this answer
 
v2
Comments
[no name] 22-Sep-14 14:41pm    
@SA, My understanding for Invoke or BeginInvoke is only for UI? Can we use them for long running back end process?
Sergey Alexandrovich Kryukov 22-Sep-14 15:48pm    
Yes; please see Solution 2.
—SA
LLLLGGGG 22-Sep-14 16:30pm    
Try even to search for async/await pattern. This is a new feature included in .NET 4.5. Some people find themselves more comfortable with this kind of pattern. Personally, I don't, but it is something you have to use if you will pass from WPF to WinRT application development.

LG
Sergey Alexandrovich Kryukov 22-Sep-14 18:44pm    
Agree. I have the same attitude: explicit threading can be more straightforward, and thread synchronization uses the same patterns over and over, they are not application-specific. But some tends to use asynchronous API approach; it could be just the cultural difference.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900