|Project:||The Open Toolkit library|
WinGLNative has a static counter that it increments each time an instance is created. It uses this counter to form the name of the window class that is registered in CreateWindow. This is necessary because window classes within a process must be unique.
The problem with this strategy is that each AppDomain gets its own copy of all statics. So the window class name is unique only within the AppDomain, not within the process. So if you use WinGLNative in two different app domains in the same process, you'll get exceptions like this:
OpenTK.PlatformException : Failed to register window class. Error: 1410
at MiniGlobe.Renderer.Device.CreateWindow(Int32 width, Int32 height)
Error code 1410 indicates that the class is already registered.
This may all sound academic, but it's actually fairly important because NUnit runs tests in a separate AppDomain, and it creates a new AppDomain for each new test run. So this bug makes it impossible to run tests multiple times without restarting NUnit.
The attached patch fixes this problem, and simplifies the code, by using a Guid for the window class name. I also eliminated the static counter completely by using the fact that the ID in a call to SetTimer only needs to be unique within a window, not within the entire process. So I just hard-coded a timer ID of 1 instead of using the static counter. The patch is hereby contributed to the public domain.