c#窗体在调用show对话框时阻止其他控件

本文关键字:其他 控件 对话框 窗体 调用 show | 更新日期: 2023-09-27 18:27:52

加载应用程序时。我使用下面的代码显示进度条。问题是,如果有人点击工具栏上下文菜单(退出的方式),它将被阻止,直到进度条关闭。有人知道实现这一目标的更好方法吗?

我之所以使用ShowDialog,是因为当我使用Show时,进度条不会产生动画——我使用的是MarqueStyle。

感谢

public partial class PopUpProgessBar : Form
{
    public PopUpProgessBar()
    {
        InitializeComponent();
    }
    Thread t;
    private void StartAnmiation()
    {
        this.Update();
        this.ShowDialog();
    }
    public void Stop()
    {
        if (t != null)
        { 
            t.Abort();
            t.Join();
        }
    }
    public void Start()
    {
        if (t == null)
        {
            t = new Thread(new ThreadStart(this.StartAnmiation));
            t.Start();
        }
    }

c#窗体在调用show对话框时阻止其他控件

这段代码看起来不太正确。你确定它不会引发跨线程冲突吗?总的来说,你在这里的整个比喻是错误的。您需要将GUI保持在GUI线程上。在后台线程上加载应用程序,并让它向GUI线程发送进度更新。

您的PopupProgressBar窗体不应该负责在新线程中加载自己,这应该在您的主窗口中完成。

我会去掉PopupProgressBar中的所有线程内容,让它简单地开始更新它的字幕。然后,在你的主窗口(OnLoad)中,你告诉它做它的事情:

        bool done = false;
        PopupProgressBar splashForm = null;
        ThreadPool.QueueUserWorkItem((x) =>
        {
            using (splashForm = new PopupProgressBar ())
            {
                splashForm.Show();
                while (!done)
                    Application.DoEvents();
                splashForm.Close();
            }
        });
        // do all your initialization work here
        // also, during each step of your initialization you could send call a function
        // in splashForm to update
        done = true;