C# - How to change window state of Form, on a different thread?
- by Dodi300
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.