Run WPF window in a separate thread

I use the following code to open a window in a separate thread

public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); } private void button1_Click(object sender, RoutedEventArgs e) { Thread newWindowThread = new Thread(new ThreadStart(() => { // Create and show the Window Config tempWindow = new Config(); tempWindow.Show(); // Start the Dispatcher Processing System.Windows.Threading.Dispatcher.Run(); })); // Set the apartment state newWindowThread.SetApartmentState(ApartmentState.STA); // Make the thread a background thread newWindowThread.IsBackground = true; // Start the thread } } 

If I use this code in a method, it works. But when I use it as follows, I get an error:

 public partial class App : Application { #region Instance Variables private Thread newWindowThread; #endregion protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); newWindowThread = new Thread(new ThreadStart(() => { // Create and show the Window Config tempWindow = new Config(); tempWindow.Show(); // Start the Dispatcher Processing System.Windows.Threading.Dispatcher.Run(); })); } private void button1_Click(object sender, RoutedEventArgs e) { // Set the apartment state newWindowThread.SetApartmentState(ApartmentState.STA); // Make the thread a background thread newWindowThread.IsBackground = true; // Start the thread } } 

It produces the following error:

 System.Threading.ThreadStateException The state of the thread was not valid to execute the operation 

What is the reason for this?

+3
multithreading c # wpf
source share
3 answers

@ d.moncada, @JPVenson, @TomTom pity everyone, differently, in @ d.moncada, your answer made me understand my true mistake, really, if you run once while my code is not working. But my problem is that I try to click button1_Click in two windows, indeed, I take a timer that calls a method with a line

  private void button1_Click(object sender, RoutedEventArgs e) 

Now the solution to my problem is Thread Detection already running in C # .net?

+2
source share

I believe the problem is that you install ApartmentState after Thread as the running one.

Try:

 public partial class App : Application { #region Instance Variables private Thread newWindowThread; #endregion protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); newWindowThread = new Thread(new Thread(() => { // Create and show the Window Config tempWindow = new Config(); tempWindow.Show(); // Start the Dispatcher Processing System.Windows.Threading.Dispatcher.Run(); })); // Set the apartment state newWindowThread.SetApartmentState(ApartmentState.STA); // Make the thread a background thread newWindowThread.IsBackground = true; } private void button1_Click(object sender, RoutedEventArgs e) { // Start the thread newWindowThread.Start(); } } 
+1
source share

why are you trying to do this? there is (almost) no reason to start the 2end UI thread inside your application ... if you want a non-modal new window, instantiate the window and call show .

Why? Because this is a very complex topic, and if you do not have huge opportunities for the development of just such behavior, you can live without it.

-3
source share

All Articles