• 大小: 49KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: C#
  • 标签: 多线程  WinForm  NET  C#  

资源简介

本示例完美演示了如何使用BackgroundWorker进行耗时的操作(如下载和数据库事务)。示例中,指定圆周率pi的结果位数,即可计算对应的pi的值,并实时输出当前计算的结果。由于采用多线程,用户体验非常好。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace AsyncCalcPiWithBackgroundWorkerSample {

  partial class AsyncCalcPiForm : Form {
    public AsyncCalcPiForm() {
      InitializeComponent();
    }

    void ShowProgress(string pi int totalDigits int digitsSoFar) {
      // Make sure we‘re on the UI thread
      Debug.Assert(this.InvokeRequired == false);

      // Display progress in UI
      this.resultsTextBox.Text = pi;
      this.calcToolStripProgressBar.Maximum = totalDigits;
      this.calcToolStripProgressBar.Value = digitsSoFar;
    }

    class CalcPiUserState {
      public readonly string Pi;
      public readonly int TotalDigits;
      public readonly int DigitsSoFar;

      public CalcPiUserState(string pi int totalDigits int digitsSoFar) {
        this.Pi = pi;
        this.TotalDigits = totalDigits;
        this.DigitsSoFar = digitsSoFar;
      }
    }

    void CalcPi(int digits) {
      StringBuilder pi = new StringBuilder(“3“ digits + 2);

      // Report initial progress
      this.backgroundWorker.ReportProgress(0
        new CalcPiUserState(pi.ToString() digits 0));

      if( digits > 0 ) {
        pi.Append(“.“);

        for( int i = 0; i < digits; i += 9 ) {
          int nineDigits = NineDigitsOfPi.StartingAt(i + 1);
          int digitCount = Math.Min(digits - i 9);
          string ds = string.Format(“{0:D9}“ nineDigits);
          pi.Append(ds.Substring(0 digitCount));

          // Report continuing progress
          this.backgroundWorker.ReportProgress(0
            new CalcPiUserState(pi.ToString() digits i + digitCount));

          // Check for cancelation
          if( this.backgroundWorker.CancellationPending ) { return; }
        }
      }
    }

    void calcButton_Click(object sender EventArgs e) {
      // Don‘t process if cancel request pending
      // (Should not be called since we disabled the button...)
      if( this.backgroundWorker.CancellationPending ) { return; }

      // If worker thread currently executing cancel it
      if( this.backgroundWorker.IsBusy ) {
        this.calcButton.Enabled = false;
        this.backgroundWorker.CancelAsync();
        return;
      }

      // Set calculating UI
      this.calcButton.Text = “Cancel“;
      this.calcToolStripProgressBar.Visible = true;
      this.calcToolStripStatusLabel.Text = “Calculating...“;

      // Begin calculating pi asynchronously
      this.backgroundWorker.RunWorkerAsync(
        (int)this.decimalPlacesNumericUpDown.Value);
    }

    void backgroundWorker_DoWork(object sender DoWorkEventArgs e) {
      //throw new Exception(“Ooops!“);

      // Track start time
      DateTime start = DateTime.Now;

      CalcPi((int)e.Argument);

      // Indicate 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-11-07 17:53  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\
     文件        4132  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiForm.cs
     文件        8297  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiForm.designer.cs
     文件        6399  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiForm.resx
     文件        3070  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiWithBackgroundWorkerSample.csproj
     文件         962  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiWithBackgroundWorkerSample.sln
     文件       14848  2013-11-07 16:50  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\AsyncCalcPiWithBackgroundWorkerSample.suo
     目录           0  2013-11-07 17:53  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\bin\
     目录           0  2013-11-07 17:53  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\bin\Debug\
     文件       24576  2013-11-07 10:12  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\bin\Debug\AsyncCalcPiWithBackgroundWorkerSample.exe
     文件       32256  2013-11-07 10:12  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\bin\Debug\AsyncCalcPiWithBackgroundWorkerSample.pdb
     文件        5632  2013-11-07 10:12  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\bin\Debug\AsyncCalcPiWithBackgroundWorkerSample.vshost.exe
     文件        3791  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\NineDigitsOfPiAt.cs
     目录           0  2013-11-07 17:53  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\
     文件        1312  2013-11-07 10:12  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\AsyncCalcPiWithBackgroundWorkerSample.csproj.FileListAbsolute.txt
     目录           0  2013-11-07 17:53  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\
     文件         180  2013-11-07 10:12  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\AsyncCalcPiWithBackgroundWorkerSample.AsyncCalcPiForm.resources
     文件         852  2013-11-07 10:12  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\AsyncCalcPiWithBackgroundWorkerSample.csproj.GenerateResource.Cache
     文件       24576  2013-11-07 10:12  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\AsyncCalcPiWithBackgroundWorkerSample.exe
     文件       32256  2013-11-07 10:12  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\AsyncCalcPiWithBackgroundWorkerSample.pdb
     文件         180  2013-11-07 10:12  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\AsyncCalcPiWithBackgroundWorkerSample.Properties.Resources.resources
     目录           0  2013-11-07 09:55  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\Refactor\
     目录           0  2013-11-07 09:36  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\obj\Debug\TempPE\
     文件         456  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Program.cs
     目录           0  2013-11-07 17:53  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\
     文件        1333  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\AssemblyInfo.cs
     文件        3313  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\Resources.Designer.cs
     文件        5612  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\Resources.resx
     文件        1119  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\Settings.Designer.cs
     文件         249  2005-12-16 16:32  AsyncCalcPiWithBackgroundWorkerWithCancelationSample\Properties\Settings.settings

评论

共有 条评论