Saturday, February 2, 2008

OSG scene viewed inside a .NET control

This is an interesting challenge. This is what I did: I created two threads, one for the main form and one for the OSG loop. This second thread shall run a method that receives a handle as a parameter. This handle can be either a handle to the form or a handle to a control (i.e. Panel) and is assigned as follows:

new osgViewer::GraphicsWindowWin32::WindowData(hwnd);

The method that is run by the second thread contains the usual instructions to render a scene in OSG. Of course, this is done in C++ and called from C#.

I run into a problem while doing this: it is not possible to access a control "from other than the thread it was created on" (Cross-thread operation error). After doing some investigation I found this can be fixed using:

CheckForIllegalCrossThreadCalls = false;

Still, this is a quick fix and I do not think is the solution for a commercial app. Visual Studio 2005 has more checking than Visual Studio 2003, therefore if you are running 2003 you might not receive this error.

No comments: