如何在不同的线程上更改窗体的窗口状态?

发布于 2024-08-27 19:02:50 字数 910 浏览 12 评论 0原文

有谁知道如何从另一个线程更改表单的窗口状态?这是我正在使用的代码:

    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;
    }

它的目的是运行一个程序并最小化,然后在程序关闭后返回到正常状态。尽管我收到此错误“跨线程操作无效:从创建它的线程以外的线程访问控制'Form1'”。知道如何让它发挥作用吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

再见回来 2024-09-03 19:02:50

这适用于 .NET 3.5:

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

或 2.0:

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

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; }));
客…行舟 2024-09-03 19:02:50

只需在 StackOverflow“跨线程操作无效”或 Google 中搜索此字符串即可。拜托,别那么懒。

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

柠檬 2024-09-03 19:02:50

请参阅 Invoke() 和 BeginInvoke() 之间的区别地点。 “选择的”答案很好地解释了你应该做什么。

长话短说,您想要不同的线程而不是完全创建一个新进程(或者您不太可能想要这样),并且您可能想要使用 Invoke() 而不是 BeginInvoke() 这是异步的。

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.

空宴 2024-09-03 19:02:50

将此行代码添加到 Click 事件处理程序中:

process.SynchronizingObject = this;

Add this line of code to the Click event handler:

process.SynchronizingObject = this;
孤芳又自赏 2024-09-03 19:02:50

这将解决您的问题,将其添加到 form_load 事件中

 System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;

this will solve your problem add it in the form_load event

 System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文