0

I have some timers as member :

Windows::UI::Xaml::DispatcherTimer^ m_pollingTimer;

I create the timer as below and on timer tick i execute some async task :

m_pollingTimer = CreateAndStartDispatcherTimer(500ms, &MainPage::OnPollingTick);

While reloading/suspending application i stop the timers as below :

Stop all timers :

for (auto timer : {m_pollingTimer})
{
    if (timer)
        timer->Stop();
}

What happens to background tasks if I stop the timer itself? - Does it stop the background tasks as well or waits for the task to finish?

Reload Application :

bool MainPage::Reload(Platform::Object^ param)
{
    auto rootFrame = dynamic_cast<Windows::UI::Xaml::Controls::Frame^>(Window::Current->Content);

    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    if (rootFrame == nullptr)
    {
        // Create a Frame to act as the navigation context and associate it with
        // a SuspensionManager key
        rootFrame = ref new Windows::UI::Xaml::Controls::Frame();
    }

    auto type = rootFrame->CurrentSourcePageType;

    try
    {
        return rootFrame->Navigate(type, param);
    }
    catch (Platform::Exception^ ex)
    {
        throw ex;
    }
}

After stopping the timers i reload the UI.

Will the background thread keep running or stopped already?

IInspectable
  • 46,945
  • 8
  • 85
  • 181
sohel14_cse_ju
  • 2,481
  • 9
  • 34
  • 55
  • 2
    When the timer is stopped, new events that initiate new background operations won't be raised - but any code running in a background thread will still run until it completes. What kinds of background tasks are you running? – Dai Nov 19 '19 at 04:48
  • concurrency::task pplwin. I am accessing some members from a const method which is initialized at application start/restart only. I am not sure if i am thread safe! – sohel14_cse_ju Nov 19 '19 at 05:02
  • 1
    C++ code that is `const`-correct is automatically thread-safe *provided* that no other code is mutating state/memory that the `const` methods are reading from ( https://stackoverflow.com/questions/14127379/does-const-mean-thread-safe-in-c11 ). If the members are only modified during startup then it will be thread-safe and there's nothing to worry about. – Dai Nov 19 '19 at 05:30
  • `for (auto timer : {m_pollingTimer})` - I'm curious why you use that pattern? – Peter Torr - MSFT Dec 03 '19 at 20:20
  • @Dai, `const`-correctness isn't really possible with foreign type systems such as WinRT that don't have the concept of `const`-ness. – Peter Torr - MSFT Dec 03 '19 at 21:18

1 Answers1

0

Existing threads will be suspended with the rest of the application. As noted in the comments, no new timer events will be raised since you manually called Stop on them.

If you have work that you want to complete before your app is suspended, you should take a Deferral on the Suspending event and then only Complete it once your background work is complete.

That ensures you won't freeze a background thread half-way through its work and then wake it up again later whilst simultaneously running your reload code. That could easily cause race conditions. Also your background work should poll periodically to see if suspension is pending and thus it should try to terminate early.

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51