1

ProgressForm 类:

public partial class ProgressForm : Form
    {
        public int prc = 0, sz;
        MainForm mf;

        public ProgressForm(MainForm MF)
        {
            InitializeComponent();
            mf = MF;
            sz = 0;
        }

        public ProgressForm(int mx)
        {
            InitializeComponent();
            sz = mx;
        }

        public void SetMax(int mx)
        {
            sz = mx;
        }

        public void StartProgress()
        {
            timer1.Enabled = true;
        }

        public void IncProgress(int prg)
        {
            prc += prg;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            double pos = (double)prc / (double)sz * 100;
            progressBar.Value = (int)pos;
        }

        private void ProgressForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Enabled = false;
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            mf.isCanceled = true;
            this.Close();
        }

        private void ProgressForm_Shown(object sender, EventArgs e)
        {
            progressBar.Value = 0;
            StartProgress();
        }

    }

MainForm 类:

void DeleteFiles()
            {
                int x = 0;
                int cnt = resultList.Count;
                isCanceled = false;

                DeleteThreadHandler("beginprogress");
                try
                {
                    DeleteThreadHandler("begindelete");
                    for (int j = 0; j < cnt; j++)
                    {
                        if (resultList[x].isChecked)
                        {
                            DeleteThreadHandler("progress");
                            DeleteFile(resultList[x].name, deleteForm.isDeletePermanently);
                            if (File.Exists(resultList[x].name))
                            {
                                DeleteErrorHandler(resultList[x].name);
                                isError = true;
                            }
                            else
                                resultList.RemoveAt(x);
                        }
                        else
                            ++x;

                        if (isCanceled)
                            break;
                    }
                }
                finally
                {
                    validity(true);
                    DeleteThreadHandler("enddelete");
                }
            }

            void DeleteErrorHandler(string val)
            {
                Action action = null;

                action = () =>
                {
                    errorReportForm.AddError(val);
                };

                this.BeginInvoke(action);
            }

            void DeleteThreadHandler(String title)
            {
                Action action = null;
                if (title == "beginprogress")
                {
                    action = () =>
                    {

                    };
                }
                else
                if (title == "begindelete")
                {
                    action = () =>
                    {
                        olvVirtual.BeginUpdate();
                    };
                }
                else
                    if (title == "enddelete")
                    {
                        action = () =>
                        {
                            olvVirtual.VirtualListSize = resultList.Count;
                            olvVirtual.EndUpdate();
                            RefreshStatus();
                            progressForm.Close();
                            if (isError)
                                errorReportForm.ShowDialog();
                        };
                    }
                if (title == "progress")
                {
                    action = () =>
                    {
                        progressForm.IncProgress(1);
                    };
                }

                this.BeginInvoke(action);
            }    


    private void DeleteBtn_Click(object sender, EventArgs e)
        {
            int checkedcount = GetCheckedCount();
            if (checkedcount == 0)
            {
                MessageBox.Show("Please mark at least a file first");
                return;
            }
            DialogResult dr = new DialogResult();
            if (deleteForm == null)
                deleteForm = new DeleteForm();
            dr = deleteForm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                //if (progressForm == null)
                progressForm = new ProgressForm(this);
                progressForm.Text = "Deleting...";
                progressForm.SetMax(checkedcount);

                if (errorReportForm == null)
                    errorReportForm = new ErrorReportForm();
                errorReportForm.ClearMemo();
                isError = false;

                Thread t = new Thread(DeleteFiles);
                t.Start();
                progressForm.ShowDialog();
            }
        }

在 progressForm 中,有一个进度条和计时器,每 500 毫秒更新一次进度。问题是我仍然可以访问主表单,我也尝试了 BeginInvoke 但都不起作用有人知道出了什么问题吗?

谢谢

编辑:我找到了这个混乱的来源,它是使用 Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile 的 DeleteFile。用非托管代码替换它后,它工作正常。

4

2 回答 2

1

尝试

 progressForm.ShowDialog(this);  // assuming this is the main form
于 2011-05-25T04:47:53.070 回答
0

很难确切地说出这是什么原因,但是通过一些调试,您将有更好的机会发现原因。

我会尝试以下方法;

  • 当您在 DeleteForm 上运行 ShowDialog 时,这是否表现为 DeleteForm 的模式对话框,或者您仍然可以在 DeleteForm 可见时单击基础表单?如果没有,当你运行 ShowDialog(this) 时会这样吗?

  • 调用 progressForm.ShowDialog(this) 并设置几个断点 (1) 在 DeleteFiles 的开头和 (2) 在行 progressForm.IncProgress(1); 当断点被命中时,使用即时窗口检查progressForm.Owner和progressForm.Modal

另外,你将当前对象的引用传递到进度表是为了什么?ProgressForm 的构造函数中是否有任何可能导致这些问题的东西?

关于 MSDN 的模态对话框的另一件事是使用关闭按钮。以下来自; http://msdn.microsoft.com/en-us/library/w61zzfwe.aspx

当窗体显示为模式对话框时,单击关闭按钮(窗体右上角带有 X 的按钮)会导致窗体隐藏并且 DialogResult 属性设置为 DialogResult.Cancel。与无模式表单不同,当用户单击对话框的关闭表单按钮或设置 DialogResult 属性的值时,.NET Framework 不会调用 Close 方法。相反,该表单被隐藏并且可以再次显示而无需创建对话框的新实例。因为显示为对话框的窗体是隐藏的而不是关闭的,所以当您的应用程序不再需要该窗体时,您必须调用该窗体的 Dispose 方法。

于 2011-05-25T06:47:47.243 回答