Search code examples
c#wpfmultithreadingmigration.net-8.0

WPF Windows Initializing is locking the separated thread in .Net 8


I have the following code that I have used for many years in .Net Framework 4.8 to show a splash screen on my startup:

      Thread newWindowThread = new Thread(new ThreadStart(() =>
      {
        WpfSplashScreen splash = new WpfSplashScreen();//Get stuck here!
        splash.Show();
        Thread.Sleep(3000);
        splash.Close();
      }));

      newWindowThread.SetApartmentState(ApartmentState.STA);
      newWindowThread.IsBackground = true;
      newWindowThread.Start();

      //Loading some things...
      Thread.Sleep(4000);

But after migrating my project to .Net 8 it stopped working like before. Now the new thread created to show the SplashScreen gets stuck at the dialog initializing and only continues after the main thread has finished, this causes my SplashScreen to only open after all loading is complete.

While debugging, if I step into 'WpfSplashScreen()' I can see that the thread stops at the 'InitializeComponent()' method.

I don't want to change my SlpashScreen to another component, that is not the idea here, I have used the same approach to open other dialogs on their own threads in many parts of my program for different reasons (like for example a progressbar window) and all cases have the same problem after migration. The SplashScreen is just the simplest example I have about this problem. So I really need to know how to make the Wpf dialog initialization not hang the separate thread in .Net 8.

Any idea?


Solution

  • I discovered that the problem was not in .Net 8, the problem is that my application is actually a plugin running in Autocad, and its initialization is done by Autocad initialization. In the recently released version 2025 of Autocad they probably made some change in Autocad initialization to prevent a secondary thread from starting during Autocad initialization. Because of this, the thread I made to show the SlpashScreen is starting only after the main thread finishes.

    Even so, I would like to thank you again because you made me understand that starting a new thread just to do simple things like this is not a good idea. I will implement the suggestions you gave in place of this approach that uses threads.