0

I've observed a bug in my UWP application where a DispatcherTimer object no longer fires its tick method when I minimize the app (and still doesn't fire when the app is restored to focus as if the event handler has been unregistered), but only when running in non-debug mode (starting the app w/out debugging). However, in debug mode, even if I minimize the app, the tick event continues to fire.

My understanding in researching other threads on SO is that DispatcherTimer runs on the UI thread. If the app is minimized and sent into a suspending state, does that stop the dispatcher loop and therefore the stopping of ticks? If so, why does that behavior not occur in debug mode?

When the debugger is attached, it ticks fine when the app is minimized.

ShrimpCrackers
  • 4,388
  • 17
  • 50
  • 76
  • _"is that DispatcherTimer runs on the UI thread. If the app is minimized...sent into...suspending state, does that stop the dispatcher loop and therefore the stopping of ticks"_ - the thread is secondary, _it is the processing of the Message Pump that determines whether or not an app is responsive or not,_ which **yes** is performed by the process's primary (better known as the "UI") **thread** in processes that have a concept of a UI. If the Message Pump is not processed for any reason (e.g. calculating prime numbers on the UI thread), the app fails to render & no timer ticks are processed. –  Dec 07 '22 at 10:51
  • ...unless of course the UWP app does the equivalent to `Application.DoEvents()` but now it has a re-entrancy problem. When you say the Release mode version fails to tick, is it a side-loaded version like the Debug version or did you install the UWP proper into Windows via an installer? It's possible Windows is automatically suspending the installed version but not the Debug-non-installed version. –  Dec 07 '22 at 10:55
  • 1
    The OS can't suspend a UWP process while it is being debugged, that would cause the debugger to become unresponsive. – Hans Passant Dec 07 '22 at 11:03
  • @Deleted No release mode build. The debug sideload/non-installed version fails to tick. Basically in Visual Studio starting the app by going to Debug -> Start without Debugging. When the debugger is attached, it ticks fine when the app is minimized. – ShrimpCrackers Dec 07 '22 at 11:25
  • Oh! I have updated your question with that. I thought you meant Debug _build_. Yes that's odd behaviour. –  Dec 07 '22 at 11:37
  • I figured out the issue. @HansPassant is correct in that when the debugger is attached, the app will not suspend when minimized because the debugger would not respond. When I was running the app without the debugger attached, it was suspending the app and essentially stopping the tick. I basically have to find out a way to restart the timers when the app is resumed. – ShrimpCrackers Dec 07 '22 at 12:03
  • You should try `System.Threding.Timer` instead of DispatcherTimer as it runs on a background thread – theemee Dec 07 '22 at 12:37

1 Answers1

0

If the app is minimized and sent into a suspending state, does that stop the dispatcher loop and therefore the stopping of ticks?

Short answer: Yes. If the app is suspended, there will be no Tick events raised.

If so, why does that behavior not occur in debug mode?

Because the debugger becomes unresponsive as explained by @HansPassant.

mm8
  • 163,881
  • 10
  • 57
  • 88