使用BackgroundWorker时,出现ProgressChanged延迟的现象,即我在西面代码的Completed事件中抛出完成时的messageBox,但是因为ProgressChanged的延迟,我的ProgressBar进度条还没满就弹出了对话框,然后progressBar才满……这问题请帮我分析下。
虽说是AsyncOperation.Post()插入消息会延迟,但是ProgressChanged消息是在Completed消息之前插入的啊,至少执行上应该保持队列去执行消息吧????
代码如下:
View Code
public partial class BackgroundWorker_Test : Form
{
private BackgroundWorker worker2 = null;
public BackgroundWorker_Test()
{
InitializeComponent();
worker2 = new BackgroundWorker();
worker2.DoWork += backgroundWorker2_DoWork;
worker2.ProgressChanged += backgroundWorker2_ProgressChanged;
worker2.RunWorkerCompleted += RunWorkerCompleted;
worker2.WorkerSupportsCancellation = true;
worker2.WorkerReportsProgress = true;
}
private void btn_Cancel_Click(object sender, EventArgs e)
{
if (worker2.IsBusy)
{
worker2.CancelAsync();
}
}
private void btn_Start_Click(object sender, EventArgs e)
{
if (!worker2.IsBusy)
{
this.progressBar1.Value = 0;
worker2.RunWorkerAsync();
}
else
{
MessageBox.Show("正在执行操作,请稍后");
}
}
private void backgroundWorker2_DoWork(object sender,
DoWorkEventArgs e)
{
BackgroundWorker worker2 = sender as BackgroundWorker;
int sum = 0;
try
{
for (int i = 1; i
1 回答
- 1 回答
- 0 关注
- 727 浏览
添加回答
举报
0/150
提交
取消