I am creating a video player application with a UI in C# and the video decoding and display (DirectX) in C++.
The C++ code is compiled as a DLL and interfaced with the 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.
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.
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.
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.
Consider using Control.Invoke() to invoke the execution you need on the main thread.
Bear in mind though that
Control.Invoke()uses Windows’ message queue to pass the request through, so expect some inaccuracies.