dotnet / dotnet/Silk.NET

Error creating multiple windows in .NET 8

Open
#2,436 8 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C#
Stars
5.2k
Forks
477
Avg merge
3d 16h
Merged PRs (30d)
2

Description

# Summary
I wanted to build a docking system using ImGui where, when the imgui window is undocked another silk.net window is created to host that tabs content. Just running the window was not possible in the same thread since the Run method would block the "main" window from running. After that I tried to move the new window into it's own thread and I noticed that something wasn't working since I was getting the following error:

`Silk.NET.GLFW.GlfwException: 'PlatformError: Win32: Failed to register window class: Class already exists. '`

I tried to reproduce the problem in a test project and using .NET 6 this works just fine. However using .NET 8 like the rest of the project, I got the same error using this code (NOTE: The 'usafe' block is just for debugging purposes, doesn't affect the problem itself):

```csharp
static void Main()
{
// Changing the WindowClass did not work.
var options1 = WindowOptions.Default with { Title = "Window 1", WindowClass = "MyWindowClass1" };
var options2 = WindowOptions.Default with { Title = "Window 2", WindowClass = "MyWindowClass2" };

var window1 = Window.Create(options1);
var window2 = Window.Create(options2);

StartWindow(window1);
StartWindow(window2); // You can uncomment this to see the program working with just one window.
}

static void StartWindow(IWindow window)
{
var thread = new Thread(() =>
{
window.Load += () =>
{
var gl = GL.GetApi(window);

unsafe
{
var versionPtr = gl.GetString(StringName.Version);
string? version = SilkMarshal.PtrToString((nint)versionPtr);
Console.WriteLine($"OpenGL version: {version}");
}

};

window.Run(); // The GlfwException is thrown here
});

thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
```

# System info:
- Platform: Desktop, Windows 11, Insider Preview 10.0.26120.1930
- Framework Version: .NET 8
- IDE: Visual Studio Community 2022 Preview, 17.14.0 Preview 2.0
- API: OpenGL
- API Version: OpenGL version: 3.3.0 NVIDIA 572.16

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.