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

Posted by Dodi300 on Stack Overflow See other posts from Stack Overflow or by Dodi300
Published on 2010-04-08T15:26:31Z Indexed on 2010/04/08 15:33 UTC
Read the original article Hit count: 315

Filed under:
|
|
|

Hello. Does anyone know how I can chage 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? Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms