Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello ,

I have studied a whole lots on dispatcher , but i have had some queries which i was asked in some interview and want to verify if i am wrong or right.

1) Can we create a ui element at runtime in a different thread in WPF? I am not clear about this question. The first question would be can we create UI elements in a different thread. If yes what would be the dispatcher scenario then.
2) Can we have more than one dispatchers in WPF Application? I think its a NO
3) Can a WPF Application have multiple UI Threads? I think NO
4) If we are launching the form using Form.Show() in WPF, will it launch another dispatcher? Again No
Posted
Comments
Sergey Alexandrovich Kryukov 9-Sep-15 11:41am    
Many your assumption turned out to be wrong, but the questions themselves are important and interesting, so I up-voted your post. Please read my answers thoroughly and ask your follow-up questions if you have any; the subject is not trivial at all.
—SA

Here are my answers, where I try to correct or clarify the answers of Solution 1:

  1. This question is more delicate than it seems. The answer of Solution 1 simplifies things. Actually, you can create an UI element in a non-UI thread, that is, call a constructor, change some properties, etc. What you cannot do is inserting these element in the UI running in a different thread. To do so, you would need to use the current dispatcher via Invoke or BeginInvoke
  2. You are wrong. Not only you can use more than one dispatcher, you are bound to have more than one, if you have more then one thread (not counting original UI thread). Here is why: you use the current dispatcher instance using the static property System.Windows.Threading.Dispatcher.CurrentDispatcher:
    https://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.currentdispatcher%28v=vs.110%29.aspx[^].

    Read this MSDN help document. You will see that the instance of dispatcher is a subject of lazy initialization. Is it different instance for each of different threads? Yes, absolutely. I checked it up, and you can check it up, too, by having more then one threads, taking the dispatcher instances and comparing them for referential identity. They are different instances; you have no choice.
  3. Yes, you can use different UI threads, but this would be impractical. You start new UI in new thread when you call Application.Run. You can have different applications in different threads. There is no need to do so. Solution 1 mistakenly claims that this is the way to create separate dispatcher instances; but in reality, dispatcher instances are separate even if you have only one UI thread; it's enough to create dispatcher instances for each non-UI thread, as I explained in previous item. The point about the modal dialog behavior is really good in Solution 1 though. The practical conclusion: use only one UI thread.

    There is a remaining aspect which we did not mention: some extra threads are implicitly created when you use WPF. For example, in addition to UI thread itself, a separate "rendering thread" is created in addition to it. File dialogs causes extra threads to be created and executed; they are used to track changes in file systems performed by other processes.
  4. Form.Show() is irrelevant to WPF. But there is a delicate moment: you can run two different UI instances in the same application. I already explained it, but here is what you can do: run WPF Application.Run in one thread and System.Windows.Forms.Application.Run in another.

    Amazingly, it works; I tried it with pure experimental purposes. But in practice it makes little to no sense. By the way, you can host System.Windows.Forms.Control in WPF. Please see: https://msdn.microsoft.com/en-us/library/vstudio/ms751761%28v=vs.100%29.aspx[^].


—SA
 
Share this answer
 
v2
Take a look at Threading Model

Quote:
1) Can we create a ui element at runtime in a different thread in WPF? I am not clear about this question. The first question would be can we create UI elements in a different thread. If yes what would be the dispatcher scenario then.


Historically, Windows allows UI elements to be accessed only by the thread that created them. This means that a background thread in charge of some long-running task cannot update a text box when it is finished. Windows does this to ensure the integrity of UI components. A list box could look strange if its contents were updated by a background thread during painting.

Quote:
2) Can we have more than one dispatchers in WPF Application? I think its a NO


Yes, we can: Every UI thread must have at least one Dispatcher, and each Dispatcher can execute work items in exactly one thread.

Quote:
3) Can a WPF Application have multiple UI Threads? I think NO


From Can/Does WPF have multiple GUI threads?

There can be multiple GUI threads (and therefor multiple Dispatcher instances).
However: Simply creating a new window (modal or not) does not create a new GUI thread. One needs to create the thread explicitly (by creating a new instance of Thread).
Note: Instead of using separate threads, modal dialogs are likely being realized by using Dispatcher.PushFrame() which blocks the caller of this method while still allowing events to be dispatched.

Quote:
4) If we are launching the form using Form.Show() in WPF, will it launch another dispatcher? Again No

See 3) above
 
Share this answer
 
v8
Comments
Sergey Alexandrovich Kryukov 9-Sep-15 11:39am    
You made some good points here. But unfortunately, each of your answers is inaccurate or plain untrue, in one or another aspect. I explained it in Solution 2 where I tried to correct your answers. However, the difference between true state of affairs and your answers is so delicate in some aspects, that I don't want to down-vote it. However, the readers are recommended to see my answer and take into account these differences.
—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