我无法让这个 BackgroundWorker 为我工作。我在这里使用m3rLinEz的示例
问题是 GUI 没有响应并且百分比没有更新。
我正在使用母版页,并且已async="true"
在内容页的标题中设置。
我还缺少其他东西吗?
ASPX 代码:
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnClick_Go" />
<asp:Label runat="server" id="textUpdate" text="0%" />
后面的代码
protected void btnClick_Go(object sender, EventArgs e)
{
BackgroundWorker bw = new BackgroundWorker();
// this allows our worker to report progress during work
bw.WorkerReportsProgress = true;
// what to do in the background thread
bw.DoWork += new DoWorkEventHandler(
delegate(object o, DoWorkEventArgs args)
{
BackgroundWorker b = o as BackgroundWorker;
// do some simple processing for 10 seconds
for (int i = 1; i <= 10; i++)
{
// report the progress in percent
b.ReportProgress(i * 10);
Thread.Sleep(1000);
}
});
// what to do when progress changed (update the progress bar for example)
bw.ProgressChanged += new ProgressChangedEventHandler(
delegate(object o, ProgressChangedEventArgs args)
{
textUpdate.Text = string.Format("{0}%", args.ProgressPercentage);
});
// what to do when worker completes its task (notify the user)
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
delegate(object o, RunWorkerCompletedEventArgs args)
{
lblSuccess.Visible = true;
});
bw.RunWorkerAsync();
}