AdrianPi's picture

GLControl + threading

Project:The Open Toolkit library
Category:support request

Hi there,

I'm using GLControl, and have a secondary thread that is creating textures.
How can I make the control's rendering context active on the second thread?

Everything I tried failed miserably, like:

glControl1.context.MakeActive(null); // Failed to make context 196608 current. Error: 6

glControl1.MakeActive(); // Failed to make context 196608 current. Error: 170


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
AdrianPi's picture


Status:open» closed

Well, been finding out what those error codes mean:

8 - > Invalid Handle
170 -> Resource Busy

glControl1.context.MakeActive(glControl1.WindowInfo) also resulted in 170 -> resource busy

I learnt that the same gl context can only be active in one thread at a given time. The primary thread should release the context before the other can make it current.

So my solution would be to have two rendering contexts sharing display lists (and textures, etc), or as I'm doing right now, creating only the bitmaps in the secondary thread and adding them to a list when they are ready so the primary thread can grab them from there and create the textures out of them.

the Fiddler's picture


Indeed, you need to call MakeCurrent(null) on the first thread before using the context in the second thread.

I'd suggest experimenting with two different contexts (one for each thread). The main issue is that Windows.Forms are not designed with multithreading in mind, which means it might be safer to create the second context using GameWindow rather than GLControl.

As long as you don't call GameWindow.Run() or GameWindow.Visible = true, the window will remain invisible and will not take significant resources. I'd suggest calling ProcessEvents() from time to time to make sure the GameWindow responds to system messages and such. Also, don't forget to call Dispose() once you are done with the second thread, to ensure the context and window handle are destroyed correctly!

iliak's picture


You can use PBO to quickly read/update texture content asynchronously. If you use a second thread *only* to update your textures, I don't think it worth it.