How to change window state of Form, on a different thread?

21,053

Solution 1

This will work in .NET 3.5:

Invoke(new Action(() => { this.WindowState = FormWindowState.Normal; }));

Or 2.0:

Invoke(new MethodInvoker(delegate { this.WindowState = FormWindowState.Normal; }));

Solution 2

Just search this string in StackOverflow "Cross-thread operation not valid" or Google. Please, don't be that lazy.

Solution 3

Add this line of code to the Click event handler:

process.SynchronizingObject = this;

Solution 4

See What’s the difference between Invoke() and BeginInvoke() on this site. The "chosen" answer gives a good explanation of what you're supposed to do.

Long story short, you want different THREADS not making a new process entirely (or highly unlikely you want that), and you probably want to use Invoke() and not BeginInvoke() which is asynchronous.

Share:
21,053
Joey Morani
Author by

Joey Morani

Updated on September 11, 2020

Comments

  • Joey Morani
    Joey Morani over 3 years

    Does anyone know how I can change the window state of a form, from another thread? This is the code I'm using:

        private void button4_Click(object sender, EventArgs e)
        {
                string pathe = label1.Text;
                string name = Path.GetFileName(pathe);
                pathe = pathe.Replace(name, "");
                string runpath = label2.Text;
                Process process;
                process = new Process();
    
                process.EnableRaisingEvents = true;
                process.Exited += new System.EventHandler(process_Exited);
    
                process.StartInfo.FileName = @runpath;
                process.StartInfo.WorkingDirectory = @pathe;
                process.Start();
                WindowState = FormWindowState.Minimized;
        }
        private void process_Exited(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }
    

    It's meant to run a program and minimize, then return to the normal state once the program has closed. Although I get this error "Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on." Any idea how to get this to work?

  • Kevin Anderson
    Kevin Anderson about 14 years
    In 2.0 you can just go Invoke(new Action<string>(delegate { this.WindowState = FormWindowState.Normal; })); as well, not MethodInvoker if you don't want to. Action<T> exists in 2.0, it's Func<> that doesn't.
  • Adam Robinson
    Adam Robinson about 14 years
    @Kevin: True, but Action (no <T>) only exists in 3.5
  • Adam Robinson
    Adam Robinson about 14 years
    @Kevin: Considering he's starting an external application and waiting for it to exit, it seems like another process is exactly what he wants (in fact, requires).
  • Kevin Anderson
    Kevin Anderson about 14 years
    That's true, with no type it's 3.5.
  • Servy
    Servy over 10 years
    This doesn't fix the problem, it hides the problem, by introducing race conditions into your code resulting in unexpected, unexplained, and inconsistent errors when running the code. As the program becomes more complex, the likelihood of problems, and the difficulty in either tracking them down or fixing the underlying problems, rises very quickly. This exception being thrown is a feature, and one that is very valuable. Disabling it is something you should virtually never be doing.