如何使用线程或backgroundWorker取消冻结表单 [英] How to unfreeze the form using the thread or backgroundWorker

查看:121
本文介绍了如何使用线程或backgroundWorker取消冻结表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
有一个类可以执行一些工作:

Good day everyone.
There is a class that performs some work:

public  class MyNew
        {
               private bool Initialize()
               {
                 this.InitializeProgressDialog();
                 return true;
               }
               private void InitializeProgressDialog()
               {
                 this.ProgressDialog = new ProgressBar();
                 this.ProgressDialog.setProgressBarLimit(this.Count);
                 this.ProgressDialog.Show();
               }
               private void CreateBMP()
               {
                 this.ProgressDialog.updateStatus("Creating something", true);
               }
               private void CreateSmall()
               {
                 this.ProgressDialog.updateStatus("Creating something new", false);
               }
               private void CreateBigger()
               {
                  int[] array;
                  array = new int[]
                  {
                     1,
                     2,
                     3,
                     4,
                     5
                  };
                   string[] array2 = new string[array.Length];
                   for (int i = 0; i < array.Length; i++)
               {
                  this.ProgressDialog.updateStatus(string.Concat(new object[]
                {
                    "Creating something else: ",
                    i + 1,
                    " of ",
                    array.Length
                }), true);


也就是说,在ProgressDialog中(通过可视化方式将组件分布在窗体上创建),通过传递进度条的标签和参数.


That is, in ProgressDialog (created visually distribute the component on the form) by passing the label and the parameters of the progressbar.

public partial class ProgressBar : Form
    {
        MyNew Breack;
        public void setProgressBarLimit(int SomeCount)
        {
            this.progressBarStatus.Maximum = SomeCount + 2;
        }
        public void updateStatus(string statusText, bool updateBar)
        {
            if (updateBar)
            {
                if (this.progressBarStatus.Value < this.progressBarStatus.Maximum)
                {
                    this.progressBarStatus.Value++;
                }
                this.progressBarStatus.Update();
            }
            if (statusText != "")
            {
                this.labelSatus.Text = statusText;
            }
            this.labelSatus.Update();
        }
        public ProgressBar()
        {
            InitializeComponent();
        }



如何取消冻结ProgressDialog表单?
事实上,在我上课时,我也从另一个班级打来电话.也就是说,这个方案
类别1> MyNew->进度栏
如果我将类MyNew称为新线程,则不会显示ProgressDialog形式.
如何直接填充进度条backgroundWorker也不了解,因为要更新我使用的表单并不是在DoWork函数中使用参数和updateStatus运行的东西,并且已经决定了如何更新表单上的进度条.



How can I unfreeze a form ProgressDialog?
The fact that my class, I also call from another class. That is, this scheme
Class 1-> MyNew-> Progressbar
If I call the class MyNew a new thread, then the form ProgressDialog is not be displayed.
How to stuff a progressbar directly backgroundWorker also do not understand, because to update the form I use is not something running in the DoWork function with parameters and updateStatus and there is already decides how to update the progress bar on the form.
May not go that way?

推荐答案

我不太了解这个问题. (我猜英语不是您的母语?我发现很难阅读您输入的内容.)但是这里的问题似乎是您在混合计算代码和显示代码.执行计算的代码(似乎是MyNew,我希望这只是您在此处匿名发布的名称!)除了提供通知属性(例如,实现INotifyPropertyChanged并在相关属性更改时在相关属性上触发PropertyChanged事件)或事件,以告知对其应用了什么视图以对其进行更新.

然后,您可以分别在UI代码中,当按下Go按钮(或生成此计算)时,创建一个进度条,并将其绑定到您创建的MyNew的属性,或者将事件处理程序绑定到您所创建的事件提供了更新进度条的方法(取决于您选择的方法).
I don''t really understand the question. (I''m guessing English isn''t your first language? I find it hard to read what you''ve typed.) But the problem here appears to be that you are mixing the calculation code and display code. The code that performs the calculation (which appears to be MyNew, I hope that is just a name you''ve anonymised for posting here!) shouldn''t care about how information about the calculation is displayed, apart from providing notifying properties (i.e. implement INotifyPropertyChanged and fire the PropertyChanged event on the relevant properties when they change) or events to tell whatever view is applied to it to update itself.

You can then, separately, in your UI code, when you press the Go button (or however this calculation is spawned) create a progress bar and either bind it to the property of MyNew you created, or bind an event handler to the event you provided which updates the progress bar (depending on which method you chose).


这篇关于如何使用线程或backgroundWorker取消冻结表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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