未调用 BackgroundWorker DoWork 函数
BackgroundWorker DoWork function is not being called
我在我的 C# WinForms 应用程序中创建了一个加载表单,该表单会在长时间的进程中显示。
我在我的加载表单中添加了一个进度条,我想更新它以向用户提供一些加载反馈。
在我的加载表单代码中,我创建了一个新的 BackgroundWorker 并添加了 DoWork 和 ProgressChanged 事件处理程序。
问题是 backgroundWorker_ProgressChanged 没有被调用。我在函数中插入了断点,但它们没有被捕获。
我哪里错了?任何帮助表示赞赏。谢谢
frmLoading:
public frmLoading()
{
InitializeComponent();
//check if bg worker is null
if (backgroundWorker == null)
{
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += backgroundWorker_DoWork;
backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
}
backgroundWorker.WorkerReportsProgress = true;
//start
backgroundWorker.RunWorkerAsync();
}
backgroundWorker_DoWork:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
backgroundWorker.ReportProgress(i);
}
}
backgroundWorker_ProgressChanged:
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//update value of progress bar
progressBar1.Value = e.ProgressPercentage;
//set the text of the progress bar
this.Text = e.ProgressPercentage.ToString();
}
以下是使用 Progress<int>
和 Task.Run
执行此操作的方法:
public frmLoading()
{
InitializeComponent();
IProgress<int> progress = new Progress<int>(p =>
{
//update value of progress bar
progressBar1.Value = p;
//set the text of the progress bar
this.Text = p.ToString();
});
Task.Run(async () =>
{
for (int i = 1; i <= 100; i++)
{
await Task.Delay(TimeSpan.FromSeconds(0.1));
progress.Report(i);
}
});
}
个人比较喜欢微软的Reactive Framework:
public frmLoading()
{
InitializeComponent();
Observable
.Interval(TimeSpan.FromSeconds(0.1))
.Take(100)
.ObserveOn(this)
.Select(p => p + 1)
.Subscribe(p =>
{
//update value of progress bar
progressBar1.Value = p;
//set the text of the progress bar
this.Text = p.ToString();
});
}
我在我的 C# WinForms 应用程序中创建了一个加载表单,该表单会在长时间的进程中显示。 我在我的加载表单中添加了一个进度条,我想更新它以向用户提供一些加载反馈。
在我的加载表单代码中,我创建了一个新的 BackgroundWorker 并添加了 DoWork 和 ProgressChanged 事件处理程序。 问题是 backgroundWorker_ProgressChanged 没有被调用。我在函数中插入了断点,但它们没有被捕获。
我哪里错了?任何帮助表示赞赏。谢谢
frmLoading:
public frmLoading()
{
InitializeComponent();
//check if bg worker is null
if (backgroundWorker == null)
{
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += backgroundWorker_DoWork;
backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
}
backgroundWorker.WorkerReportsProgress = true;
//start
backgroundWorker.RunWorkerAsync();
}
backgroundWorker_DoWork:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
backgroundWorker.ReportProgress(i);
}
}
backgroundWorker_ProgressChanged:
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//update value of progress bar
progressBar1.Value = e.ProgressPercentage;
//set the text of the progress bar
this.Text = e.ProgressPercentage.ToString();
}
以下是使用 Progress<int>
和 Task.Run
执行此操作的方法:
public frmLoading()
{
InitializeComponent();
IProgress<int> progress = new Progress<int>(p =>
{
//update value of progress bar
progressBar1.Value = p;
//set the text of the progress bar
this.Text = p.ToString();
});
Task.Run(async () =>
{
for (int i = 1; i <= 100; i++)
{
await Task.Delay(TimeSpan.FromSeconds(0.1));
progress.Report(i);
}
});
}
个人比较喜欢微软的Reactive Framework:
public frmLoading()
{
InitializeComponent();
Observable
.Interval(TimeSpan.FromSeconds(0.1))
.Take(100)
.ObserveOn(this)
.Select(p => p + 1)
.Subscribe(p =>
{
//update value of progress bar
progressBar1.Value = p;
//set the text of the progress bar
this.Text = p.ToString();
});
}