button_click 功能工作时无法最小化窗口 [英] can't minimize the window while the button_click function working

查看:27
本文介绍了button_click 功能工作时无法最小化窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 C# 编写的带有窗口的程序.

I have a program I wrote in C# with a window.

我有一个按钮可以做一些事情(具体是什么无关紧要)并刷新他循环中的窗口(在 button_click 函数中)(使用 this.Invalidate(false); (我不使用这个.刷新,因为我有一个不想刷新的 groupBox)).

I have a button that do some things (it doesn't matter what exactly) and it refresh the window in his loop (in button_click function) (with this.Invalidate(false); (I don't use this.Refresh because I have a groupBox that I don't want to refresh)).

当 button_click 功能工作时,我无法最小化窗口.我可以做些什么来解决它?

I cant minimize the window while the button_click function working. there is something I can do to solve it?

**假设我有这个代码:

**lets say I have this code:

    void button_click(object sender, EventArgs e)
    {
        progressBar1.Value = 0;
        progressBar1.Maximum = int.Parse(somelabel_num.Text);
        int i;
        OpenFileDialog file = new OpenFileDialog();
        file.ShowDialog();
        if (file.FileName == "")
            return;
        this.Refresh();
        Bitmap image = new Bitmap(file.FileName);
        groupBox1.BackgroundImage = image;

        for (i = 0; i < int.Parse(somelabel_num.Text); i++)
        {
            this.Text = i;
            this.Invalidate(false);
            progressBar1.PerformStep();
        }
    }

那么如何作为获取参数的线程来执行此操作?

so how to do this as a thread that gets the paremeters?

推荐答案

正如已经建议的,您应该考虑在 .NET 中以多种方式之一运行按钮任务(线程、线程池、任务等).

As already suggested, you should consider running button task in one of many ways you can in .NET (Thread, ThreadPool, Task etc).

但是,如果您正在寻找快速解决方案,您可以尝试以下方法之一:

However, if you are looking for a quick fix you can try one of these things:

  1. 调用 Application.DoEvents() 以允许 UI 处理窗口消息泵.例如

  1. Call Application.DoEvents() to allow UI to process window message pump. e.g.

WindowState = FormWindowState.Minimized;
Application.DoEvents();

  • 异步调用最小化例如

  • Call minimization asynchronously e.g.

    if (InvokeRequired)
        BeginInvoke(
            new Action(() => WindowState = FormWindowState.Minimized)
        );
    

  • 这篇关于button_click 功能工作时无法最小化窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆