问题描述
本人C#新手,网上找到一段代码适合VB.net的,如下:#Region"---------------再开一个线程显示进度条---------------"PrivateSubwaitstart()Me.Cursor=Cursors.WaitCursorBackgroundWorker1.RunWorkerAsync()EndSubPrivateSubwaitend()BackgroundWorker1.CancelAsync()Me.Cursor=Cursors.DefaultEndSubPrivateSubBackgroundWorker1_DoWork1(ByValsenderAsObject,ByValeAsSystem.ComponentModel.DoWorkEventArgs)HandlesBackgroundWorker1.DoWorkDimworkerAsSystem.ComponentModel.BackgroundWorker=CType(sender,System.ComponentModel.BackgroundWorker)showProgress(worker)EndSubPrivateSubshowProgress(ByValworkerAsSystem.ComponentModel.BackgroundWorker)DimprogressFormAsNewfrmTaskProgress()progressForm.Show()progressForm.Refresh()WhileNotworker.CancellationPendingIfprogressForm.ProgressIndicator.Value=100ThenprogressForm.ProgressIndicator.Value=0ElseprogressForm.ProgressIndicator.Value+=10EndIfSystem.Threading.Thread.Sleep(50)EndWhileprogressForm.ProgressIndicator.MarqueeAnimationSpeed=0progressForm.Hide()progressForm.Dispose()EndSub#EndRegion
主要是遇到一些耗时程序时出现一个进度条提示用户等待:waitstart()耗时程序……waitend()现在把这代码转到C#就不好用了:#region"---------------再开一个线程显示进度条---------------"privatevoidwaitstart(){this.Cursor=Cursors.WaitCursor;backgroundWorker1.RunWorkerAsync();}privatevoidwaitend(){backgroundWorker1.CancelAsync();this.Cursor=Cursors.Default;}privatevoidshowProgress(System.ComponentModel.BackgroundWorkerworker){frmTaskProgressprogressForm=newfrmTaskProgress();progressForm.Show();progressForm.Refresh();while(!worker.CancellationPending){if(progressForm.ProgressIndicator.Value==100){progressForm.ProgressIndicator.Value=0;}else{progressForm.ProgressIndicator.Value+=10;}System.Threading.Thread.Sleep(50);}progressForm.ProgressIndicator.MarqueeAnimationSpeed=0;progressForm.Hide();progressForm.Dispose();}privatevoidbackgroundWorker1_DoWork(objectsender,DoWorkEventArgse){System.ComponentModel.BackgroundWorkerworker=(System.ComponentModel.BackgroundWorker)sender;showProgress(worker);}#endregion
请指点是哪里出问题了?
解决方案
解决方案二:
UpdateProgress进度条显示与取消异步功能//取消执行异步代码<scripttype="text/javascript"language="javascript">//取得PageRequestManager类的实例varpageManager=Sys.WebForms.PageRequestManager.getInstance();//设置当引发initializeRequest事件时便运行事件处理函数initializeRequestpageManager.add_initializeRequest(initializeRequest);functioninitializeRequest(sender,args){if(pageManager.get_isInAsyncPostBack()){args.set_cancel(true);}}//如果当前正在处理一个异步回发而且此异步回发是由“取消加载...”按钮所引发的话,//就调用PageRequestManager实例的abortPostBack方法来取消异步回发。functionAbortPostBack(){if(pageManager.get_isInAsyncPostBack()){pageManager.abortPostBack();}}</script>//取消加载按钮<asp:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><asp:LabelID="lbTimer"runat="server"></asp:Label><br/><asp:ButtonID="btnCommit"runat="server"Text="单击我,将开始加载Web窗体页的数据..."onclick="btnCommit_Click"/></ContentTemplate></asp:UpdatePanel></div><asp:UpdateProgressID="UpdateProgress1"runat="server">//延迟5秒进度条消失protectedvoidbtnCommit_Click(objectsender,EventArgse){System.Threading.Thread.Sleep(5000);//延时5秒钟lbTimer.Text=DateTime.Now.ToLongTimeString();//5秒后进度条将消失}//加载进度条<asp:UpdateProgressID="UpdateProgress1"runat="server"><ProgressTemplate><divstyle="font-size:xx-small">服务器加载中...<imgsrc="wait.gif"/><inputid="cmdCancel"onclick="AbortPostBack()"type="button"value="取消加载..."/></div></ProgressTemplate></asp:UpdateProgress></form>
解决方案三:
也许你没有把backgroundWorker1_DoWork挂在你的backgroundWorker1.DoWork事件上?