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?

poke
  • 369,085
  • 72
  • 557
  • 602
Cyberguille
  • 1,552
  • 3
  • 28
  • 58

3 Answers3

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?

Community
  • 1
  • 1
Cyberguille
  • 1,552
  • 3
  • 28
  • 58
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();
    }
}
d.moncada
  • 16,900
  • 5
  • 53
  • 82
-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.

Venson
  • 1,772
  • 17
  • 37
  • 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 '14 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 – Cyberguille Apr 25 '14 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 '14 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 '14 at 16:09
  • @Olivier not comment to the last one ;-). Chrome is a good example that every rule has an exception, but it is also possible to live without that ( Firefox, IE, Opera, ect ... ) Google has a very big budget and a lot time for this feature but other browsers can live without that and are also very good. Just because you can, you'r not must. Personally, I focused the last half a year in .net especially thredding, and working asnyc in WPF and even now i would say no one should start like this. – Venson Apr 25 '14 at 16:19
  • 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. – Bura Chuhadar Apr 25 '14 at 20:27
  • I think if MS have chosen to explain how to do this in their documentation then the need is considered to be great enough. https://msdn.microsoft.com/library/ms741870(v=vs.100).aspx (See the 'Multiple Windows, Multiple Threads' section). – Holf Mar 22 '16 at 14:39