简体   繁体   English

如何从辅助线程在视频播放器的主窗口中显示

[英]How to display in a Primary window from a secondary thread for a video player

I am creating a video player application with a UI in C# and the video decoding and display (DirectX) in C++. 我正在使用C#中的UI和C ++中的视频解码与显示(DirectX)创建视频播放器应用程序。

The C++ code is compiled as a DLL and interfaced with the C# UI. C ++代码被编译为DLL,并与C#UI接口。

To maintain the correct display frame rate I need to create a secondary thread either in C++ DLL or C# which can do accurate timing and call the display function at right intervals. 为了保持正确的显示帧速率,我需要在C ++ DLL或C#中创建一个辅助线程,该辅助线程可以执行准确的计时并以正确的间隔调用显示函数。

However, creating a secondary thread which posts display to the window created by the primary thread (from C# GUI) creates access violation and results in a crash. 但是,创建将显示发布到主线程创建的窗口(从C#GUI)的辅助线程会导致访问冲突,并导致崩溃。

However, if I use a timer object in C# to display, the player works but I am unable to maintain the right frame rate due to it's coarse granularity. 但是,如果我在C#中使用计时器对象进行显示,则播放器可以工作,但是由于其粗粒度,我无法保持正确的帧速率。

What would be a good solution to handle this? 有什么好办法解决这个问题?

I think the crashes you experience are caused by the fact that you can't access Windows Forms controls from outside the main thread. 我认为您遇到的崩溃是由于您无法从主线程外部访问Windows Forms控件引起的。

Consider using Control.Invoke() to invoke the execution you need on the main thread. 考虑使用Control.Invoke()在主线程上调用所需的执行。

Bear in mind though that Control.Invoke() uses Windows' message queue to pass the request through, so expect some inaccuracies. 请记住,尽管Control.Invoke()使用Windows的消息队列来传递请求,所以请注意一些不准确之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM