开发者

Dispatcher Thread and UI Rendering Thread working when WPF Form is in Minimized Stat

开发者 https://www.devze.com 2023-04-07 13:04 出处:网络
I have two question regarding WPF Dispatcher Thread. Does dispatcher thread keeps running when your WPF form is in minimized stat.

I have two question regarding WPF Dispatcher Thread.

  1. Does dispatcher thread keeps running when your WPF form is in minimized stat.
  2. I have so many dynamic updated so I flow my updated till winform and then user Timer in winform to update UI. In winform when you minimized winform your Timer will stop working as it is created in UI thread. So my CPU utilization is low. If I have to achieve same behavior in WPF MVVM then how can I achieve tha开发者_如何学JAVAt?


Both your questions seem to assume that minimizing your app will somehow stop some of your threads. I don't know where you got that idea, but it's completely wrong. Your UI thread is still running even when you're minimized. How else would it ever process the "restore" command when you un-minimize it? How would Windows be able to get its title to show on the taskbar?

You also seem to think that WPF has a "dispatcher thread" that's somehow special. That's not the case. The Dispatcher takes the place of the Windows message queue, which means it's attached to the UI thread. The thing you call a "dispatcher thread" is the same thing as the UI thread. If you're doing WinForms and WPF in the same app, they both run on the same UI thread (unless you're manually starting new threads and starting your own dispatchers on them, but that's a pretty unusual scenario).

And no, your Timer does not stop running just because your app is minimized (unless you manually wrote code to stop it). Try it: add a call to Console.Beep() in your timer tick event, and then try minimizing your app and see for yourself whether it keeps making noise.

Here's my guess: in your timer's Tick event, your WinForms app calls Invalidate(). When the app is minimized, Invalidate does nothing -- the window isn't shown, so there's nothing to invalidate -- so you see low CPU usage because it's not doing anything.

If you want the same behavior in WPF, your best bet is to add this code to the beginning of your Tick event:

if (WindowState == WindowState.Minimized)
    return;
0

精彩评论

暂无评论...
验证码 换一张
取 消