用BackgroundWorder 加 progressbar 常用于在頁面長時間處理事件中顯示進度條.
WorderReportProgress \ WorkerSupportCancellation 屬性設置為True.
1、调用BackgroundWorker的RunWorkerAsync方法(可以传递参数),它将调用DoWork事件
2、在DoWork的事件响应代码中调用耗时的操作.
3、在耗时操作中判断CancellationPending属性,如果为false则退出
4、如果要向用户界面发送信息,则调用BackgroundWorker的ReportProgress方法,它将调用ProgressChanged事件(可以将改变通过object类型传递)
5、在ProgressChanged事件的响应代码中将改变呈现给用户
6、如果需要取消耗时操作,则调用BackgroundWorker的CancelAsync方法,需要和步骤3一起使用
例如:
/// <summary>/// do work event. 耗時事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void bgWorker_DoWork(object sender, DoWorkEventArgs e){//export to excelstring[] tableName = { "WI", "WI_Details" };exporttoexcel.ExportExcel(ds, false, this.bgw, tableName);// frmMain.form1.bgWorker.ReportProgress(percent); //在耗時事件ExportExcel 中間,報告進度,這時會調用 bgWorker_ProgressChanged事件 }/// <summary>/// 耗時事件中進度條變化/// </summary>/// <param name="sender"></param>/// <param name="e"></param>public void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e){ pgBar.Value = e.ProgressPercentage; }/// <summary>/// 耗時事件完成后/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){bgWorker.Dispose();pgBar.Value = 0;pgBar.Visible = false;}//-------------------------------------------------class ExportToExcel
{/// <summary>/// export the dataset to excel/// </summary>/// <param name="ds">the dataset will be exported to excel</param>/// <param name="oddEvenColor">set the odd and event rows have different color. </param>/// <param name="bgWorker">the name of BackgroundWorker. yes</param>/// <param name="tableName">sheets 's name </param>/// public void ExportExcel(DataSet ds, bool oddEvenColor, object bgWorker,string[] tableName){if (ds == null || ds.Tables .Count == 0) return;try{Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();if (xlApp == null){return;}System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add();int rowRead = 0;int rowCount = 0;//get the table's rows countforeach (DataTable dt in ds.Tables){rowCount += dt.Rows.Count;}for (int tbi = 0; tbi < ds.Tables.Count; tbi++){Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet) workbook.Worksheets.Add();worksheet.Name = tableName[tbi].ToString();if (worksheet == null)//工作薄中没有工作表{return;}Microsoft.Office.Interop.Excel.Range range; int percent = 0;//title column namefor (int i = 0; i < ds.Tables[tbi].Columns.Count; i++){worksheet.Cells[1, i + 1] = ds.Tables[tbi].Columns[i].ColumnName;range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];range.Interior.ColorIndex = 31;range.Font.Size = 12;range.Font.Bold = true;range.Borders.LineStyle = 1;range.Font.ColorIndex = 2;//white }//content for (int r = 0; r < ds.Tables[tbi].Rows.Count; r++){if (bgWorker != null && (bgWorker as BackgroundWorker).CancellationPending)//if (frmMain.form1.bgWorker.CancellationPending)//如果有取消則退出導出 break;for (int i = 0; i < ds.Tables[tbi].Columns.Count; i++){worksheet.Cells[r + 2, i + 1] = ds.Tables[tbi].Rows[r][i].ToString();}rowRead++;percent = ((int)(100 * rowRead)) / rowCount;if (bgWorker != null && percent <= 100){(bgWorker as BackgroundWorker).ReportProgress(percent);//frmMain.form1.bgWorker.ReportProgress(percent); }}range = worksheet.Range(worksheet.Cells[2, 1], worksheet.Cells[ds.Tables[tbi].Rows.Count + 1, ds.Tables[tbi].Columns.Count])range.Borders.LineStyle = 1;if (oddEvenColor){if (r % 2 == 1)range.Interior.ColorIndex = 34;//隔行換底色}range.Font.Size = 9;worksheet.Columns.AutoFit(); //列寬自適應}xlApp.Visible = true;}catch (Exception e){throw (e);}}