c# - 当我显示它时,我的自定义 progressBar 表单挂起

标签 c# visual-studio

我制作了一个表单,它起到了进度条的作用,这是我制作的代码

  public partial class PXProgressBar : Form
    {
        public delegate bool CancelEvent();
        public event CancelEvent cancel_e;

        public Boolean ProcessCancelled
        {
            get;
            set;
        }
        public PXProgressBar(bool EnableCancel) 
        {
            InitializeComponent();
            ProcessCancelled = false;
            progressBar1.Minimum = 0;
            if (!EnableCancel)
                Cancelbtn.Visible = false;

        }
        public  void increament(int step)
        {
            if (progressBar1.Value < progressBar1.Maximum-1)
            {
                progressBar1.Value++;
                progressBar1.Caption = progressBar1.Value.ToString() + " of " + progressBar1.Maximum;
                progressBar1.Refresh();

            }

            else
            {
                progressBar1.Value++;
                progressBar1.Caption = progressBar1.Value.ToString() + " of " + progressBar1.Maximum;
                if (this.TopMost)
                    this.TopMost = false;
                this.Update();
                this.Hide();
                this.WindowState = FormWindowState.Minimized;
               // this.Dispose();
            }

        }
        public void SetMaximum(int MaximumValue)
        {
            if (MaximumValue <= 0)
            {
                progressBar1.Maximum = 0;
                return;
            }
            if (progressBar1.Minimum != 0 && MaximumValue < progressBar1.Minimum)
            {
                progressBar1.Maximum = progressBar1.Minimum;
                return;
            }
            progressBar1.Maximum = MaximumValue;


        }
        public void SetMinimum(int MinimumValue)
        {
            progressBar1.Value = 0;
            if (MinimumValue <= 0)
            {

                progressBar1.Minimum = 0;
                return;
            }
            if (progressBar1.Maximum != 100 && MinimumValue > progressBar1.Maximum)
            {
                progressBar1.Minimum = progressBar1.Maximum;
                return;
            }
            progressBar1.Minimum= MinimumValue;
        }
        public void SetTitle(string ProcessTitle)
        {
            this.ProgressTitlelb.Text =ProcessTitle;// ProcessTitle;
            //this.ProgressTitlelb.Left = (this.panel1.Width - this.ProgressTitlelb.Width) / 2;
            //this.ProgressTitlelb.Top = (this.panel1.Height - this.ProgressTitlelb.Height) / 2;
            this.Update();
        }

        private void Cancelbtn_Click(object sender, EventArgs e)
        {

            ProcessCancelled = true;
            bool disposeRequired =cancel_e();
            if(disposeRequired)
             this.Dispose();
        }

        private void PXProgressBar_Shown(object sender, EventArgs e)
        {
            this.Update();
        }

    }

我通过这段代码调用表单

  if (ProgressBar == null)
   ProgressBar = new PXProgressBar(true);
   ProgressBar.SetTitle("Saving ...");
   ProgressBar.SetMinimum(0);
   ProgressBar.SetMaximum(100);
   ProgressBar.TopMost = true;
   ProgressBar.Show();
   Application.DoEvents();

关于过去几行是在一个通过线程调用的函数中 但是当我运行它时表单挂起所以我不能在表单中设置一个取消按钮让用户取消操作

最佳答案

您的代码看起来应该没问题,所以我只能假设您在 UI 线程上执行长时间运行的操作,这会导致 UI 看起来像挂起。您需要在后台线程上执行长时间运行的操作,以便 UI 线程保持足够的响应能力以响应按钮点击等。如果您咨询您的 friend Google,有很多关于此的文章。

更多信息请点击此处 http://www.idevforfun.com/index.php/2010/01/10/windows-ui-threading/

我同意 Skizz 对 DoEvents 调用的看法……只有极少数情况下才需要该调用,而且大多数情况下它是在框架本身中使用的。

关于c# - 当我显示它时,我的自定义 progressBar 表单挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7402280/

相关文章:

c# - 使用 NUnit 测试调用私有(private)方法的公共(public)方法

c# - 在 C# 中访问/解析 "msDS-AllowedToActOnBehalfOfOtherIdentity"AD 属性

c# - asp.net:如何防止用户多次发布相同的数据

c++ - <inherit from parent or PROJECT DEFAULTS> 的默认值的 Visual Studios(主要是 2012 pro)文件位置

c++ - 在哪里可以找到 .vcproj 文件结构的引用?

c# - 从(多个)用户输入(应用程序密码)中获取数据库密码

c# - 从二维List中删除编号为N的行和列

c++ - 即使在 C++ 中没有反射,visual studio intellisense 如何识别类中的函数和属性?

c++ - 字符串比较在 Visual C++ 2005 中不起作用

c++ - FillConsoleOutputCharacter/WriteConsoleOutput 和特殊字符