C# BackgroundWorker class

本文关键字:class BackgroundWorker | 更新日期: 2023-09-27 18:09:16

目前我正在尝试更新我的进度条,如果后台工作人员报告一些东西,这里是我的代码

Form1.cs

namespace YTD
{
    public partial class Form1 : Form
    {
        private Main app;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int n;
            bool isNumeric = int.TryParse(numberBox.Text, out n);
            if (!String.IsNullOrWhiteSpace(emailBox.Text) && !String.IsNullOrWhiteSpace(passBox.Text) && !String.IsNullOrWhiteSpace(numberBox.Text) && isNumeric)
            {
                this.app = new Main(emailBox.Text, passBox.Text, n, logBox, statusBar, backgroundWorker1);
                this.app.startMule();
            }
            else
            {
                MessageBox.Show("Please fill out all the form fields", "MuleMaker error");
            }
        }
    }
}

和Main.cs

namespace YTD.classes
{
    public class Main
    {
        private String email;
        private String password;
        private int number;
        private RichTextBox logBox;
        private ProgressBar statusBar;
        private BackgroundWorker threadWorker;
        public Main(String email, String password, int number, RichTextBox logBox, ProgressBar statusBar, BackgroundWorker threadWorker)
        {
            // Define some variables
            this.email = email;
            this.password = password;
            this.number = number;
            this.logBox = logBox;
            this.statusBar = statusBar;
            this.threadWorker = threadWorker;
        }
        public void startMule()
        {
            // Set progressbar 100% value
            statusBar.Maximum = this.number;
            if (!threadWorker.IsBusy)
            {
                threadWorker.RunWorkerAsync();
            }
        }
        private void threadWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= 10; i++)
            {
                // Perform a time consuming operation and report progress.
                MessageBox.Show("ye");
                System.Threading.Thread.Sleep(500);
                threadWorker.ReportProgress(i * 10);
            }
        }
        private void threadWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            statusBar.Increment(1);
        }
    }
}

目前我没有得到错误,但进度条值没有被改变。没有后台工作器,我可以很好地更新我的进度条,但不是在做一个昂贵的动作。

C# BackgroundWorker class

你发布的代码没有显示,如果你注册了你的函数BackgroundWorker事件。

创建一个新的BackgrounWorker是不够的。下面是一个例子:

public Class Main
{
    public Main( ... )
    {
        BackgroundWorker worker = new BackgroundWorker()
        worker.WorkerReportsProgress = true;
        // Register to BackgroundWorker-Events
        worker.DoWork += threadWorker_DoWork;
        worker.ProgressChanged += threadWorker_ProgressChanged;
    }
}

另外,你应该告诉你的进度条进行渲染。

private void threadWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    statusBar.Increment(1);
    statusBar.Invalidate(true);
}

至少你可能想使用你在调用ReportProgress(i * 10)时设置的值。

private void threadWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    statusBar.Value = e.ProgressPercentage;
    statusBar.Invalidate(true);
}