Windows 8 APP,用数据绑定更改XAML文本框的文本...游戏循环工作者线程导致问题

时间:2014-03-19 23:49:41

标签: c++ xaml threadpool

我使用XAML使用Direct3D创建了一个新项目(所以我最后可以有广告)......

我有一个文本块,并将其绑定到viewmodel中的属性。

<SwapChainPanel x:Name="swapChainPanel">
    <TextBlock x:Name="debugText"  HorizontalAlignment="Left" Margin="204,658,0,0" TextWrapping="Wrap" Text="{Binding Debug}" VerticalAlignment="Top" />
</SwapChainPanel>

如果我在我的XAML.cpp文件中,并将属性值更改为某些内容,则一切正常......

this->DataContext = m_deviceResources->directXPageViewModel;

m_deviceResources->directXPageViewModel->Debug = "YUMMY";
m_deviceResources->directXPageViewModel->Update( ); // runs property changed notifier

如果我从游戏循环中更改属性,屏幕会变为空白并且没有明显的错误?

// So the same code as above (without this->Datacontext) placed inside my game class.

这似乎是因为游戏类渲染循环位于工作线程中:

// If the animation render loop is already running then do not start another thread.
if (m_renderLoopWorker != nullptr && m_renderLoopWorker->Status == AsyncStatus::Started)
{
    return;
}

// Create a task that will be run on a background thread.

//IF I PLACE THE CODE HERE, IT WORKS FINE
auto workItemHandler = ref new WorkItemHandler([this](IAsyncAction ^ action)
{   
    // BUT IF HERE, IT SHOWS BLACK SCREEN AND STOPS LOOPING
    m_deviceResources->directXPageViewModel->Debug = "YUMMY";
    m_deviceResources->directXPageViewModel->Update( );

    // Calculate the updated frame and render once per vertical blanking interval.
    while (action->Status == AsyncStatus::Started)
    {
        critical_section::scoped_lock lock(m_criticalSection);
        Update(); // I'D REALLY LIKE TO PUT THE CODE IN HERE
        if (Render())
        {
            m_deviceResources->Present( );
        }
    }
});

// Run task on a dedicated high priority background thread.
m_renderLoopWorker = ThreadPool::RunAsync(workItemHandler, WorkItemPriority::High, WorkItemOptions::TimeSliced);

当我需要处于正确的线程(我猜)时,如何在每个循环中调用更新函数

修改:::::::::::::::::::::::::

我的代码现在看起来像这样

// Create a task that will be run on a background thread.
auto workItemHandler = ref new WorkItemHandler([this](IAsyncAction ^ action)
{


    // Calculate the updated frame and render once per vertical blanking interval.
    while (action->Status == AsyncStatus::Started)
    {
        critical_section::scoped_lock lock(m_criticalSection);
        Update( );


        // THIS IS THE NEW CALL BUT IT CRASHES MY GAME :)
        CoreWindow::GetForCurrentThread( )->Dispatcher->RunAsync( CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler( [ this ]( )
        {
            m_deviceResources->directXPageViewModel->Debug = "YUMMY";
            m_deviceResources->directXPageViewModel->Update( );
        } ) );

        if (Render())
        {
            m_deviceResources->Present( );
        }
    }
});

这有一些问题,而且很难找到究竟发生了什么......我认为CoreDispatcher是NULL或其他东西所以它崩溃了:

Unhandled exception at 0x00BB84C7 in Game.exe: 0xC0000005: Access violation reading location 0x00000000.

directXPageViewModel.h

1 个答案:

答案 0 :(得分:1)

看一下CoreDispatcher课程。您应该将用于更新UI的代码放在lambda中,然后将该lambda传递给调度程序的RunAsync方法。