locked
Exception when creating a DispatcherTimer

    Question

  • I'm trying to use a timer in a Metro-style DirectX 2D game, and when it does the ref new DispatcherTimer it throws an exception, access violation.

    I thought the same code worked a few days ago before some updates, but it certainly does not work now.

    This is in a Metro-style DirectX 2D game. At this point, I am wondering if DispatcherTimer works with Direct2D.

    Is there a restriction on the type of objects that can create a timer? Was something broken in a recent update?

    I have set a breakpoint on the ref new DispatcherTimer line, and that is what causes the exception, as it occurs when I step over that line.

    To eliminate any possibility that my other code was the problem, I used Visual Studio 11 to create a new Metro-style Direct2D application, and added the following line to Initialize:

    Windows::UI::Xaml::DispatcherTimer^ timer = ref new Windows::UI::Xaml::DispatcherTimer();

    The program crashes when I step over this line.



    Wednesday, April 11, 2012 1:52 AM

Answers

  • The error message given there explains what is going on:

    First-chance exception at 0x759956C4 in Direct2DTest.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x0103F530. HRESULT:0x8001010E The application called an interface that was marshalled for a different thread.

    The DispatcherTimer needs to be created on the dispatcher thread. As you can guess from its location in Windows::UI::Xaml, the DispatcherTimer is designed for Xaml apps and not for D2D apps. You will likely be better off using a ThreadPoolTimer instead.

    --Rob

    • Marked as answer by sb5k Thursday, April 12, 2012 3:11 AM
    Wednesday, April 11, 2012 9:04 PM
    Owner

All replies

  • The error message given there explains what is going on:

    First-chance exception at 0x759956C4 in Direct2DTest.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x0103F530. HRESULT:0x8001010E The application called an interface that was marshalled for a different thread.

    The DispatcherTimer needs to be created on the dispatcher thread. As you can guess from its location in Windows::UI::Xaml, the DispatcherTimer is designed for Xaml apps and not for D2D apps. You will likely be better off using a ThreadPoolTimer instead.

    --Rob

    • Marked as answer by sb5k Thursday, April 12, 2012 3:11 AM
    Wednesday, April 11, 2012 9:04 PM
    Owner
  • This seems to work, based on the debugger. I need to write more of the game to be sure.

    Note I have a hard time finding the important information about exceptions in the current version of Visual Studio 11, but that is for another forum.

    Thank you

    Thursday, April 12, 2012 3:12 AM