3

I'm using the following code for 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 throws the following error:

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

What is the cause of this?

1
  • do you have clicked only once ? (a thread cannot be started twice)
    – Olivier
    Apr 25, 2014 at 15:35

3 Answers 3

2

@d.moncada, @JPVenson, @TomTom sorry for everyone, espetially to @d.moncada, your answer made me realize my true error, indeed if run once until my code works. But my really problem is that i try to push button1_Click in two ocation, really i take a timer that called a method with the line of the

 private void button1_Click(object sender, RoutedEventArgs e)

Now the solution of my problem is Detecting a Thread is already running in C# .net?

1

I believe the problem is that you are setting the ApartmentState after the Thread as started running.

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();
    }
}
-4

why you trying to do this? there is (nearly)no reason to run a 2end UI thread inside you application ... if you want a non Modal new Window, instantiate you window and call show.

Why nearly? Because this is a very complex topic and unless you have a huge budged to develop exactly this behavior, you can live without it.

7
  • 7
    -1. NO REASON? Ever seen a financial trading application running 6 screens? Great to have one independent message pump per screen (which is a window). The good thing about separate threads for separate windows is that all the "the UI is single threaded" then applies on a per window bassis, which can help paralellizing a LOT. So yes, there are reason - they are edge reasons and I grant you they do not apply to most programs, but someone trying to do so may well be one of the "I need it" people. Most people are not even aware this is possible.
    – TomTom
    Apr 25, 2014 at 15:41
  • @JPVensonthis not is my code complete, i need that the main window continue executed other code and throw this window with a message of alert, and use a window and no a messagebox because the idea is show a big window with big letter and color strong Apr 25, 2014 at 15:41
  • 1
    Just a matter of "skill" ... i'm working a a company where we deal with giabytes of data but just with 2 very complex Windows and everything is at a very high performance level( with Thredding but not like "putting everything in a Thread and be happy" ^^). If your application realy needs a thread for every singel UI rendering, you should maybe reconsider/refactor you software Desgin. And if you need complex UI rendering you create/modify your data in a Thread and then let the UI render. There is in 90 percent a better way then just put things into a Thread.
    – Venson
    Apr 25, 2014 at 15:45
  • 1
    @JPVenson : Hum, you may suggest to chrome dev team to refactor their code, then... chrome is not only using one thread per UI, but one process per UI. Relying on good behavior of other's code is not really robust nor sustainable. Unless everyone in your company is a f***g good programmer :)
    – Olivier
    Apr 25, 2014 at 16:09
  • 1
    @JPVenson : I don't know the size of the Guillermo's application but if the UI is rendering fine and won't be a problem in the future then there is no reason to run a STA for the UI so I agree with you. But if the UI has a performance which I doubt, then you can use STA threads. So I think people are just copying the code from other websites and writing applications like that. Apr 25, 2014 at 20:27

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.