A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© aisini 金牌黑马   /  2014-8-20 14:39  /  927 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Windows.Forms;
  6. using System.Threading;
  7. using System.Threading.Tasks;

  8. namespace AskAnswers
  9. {
  10.     public class ShowProgressStatus : Form
  11.     {
  12.         [STAThread]
  13.         static void Main()
  14.         {
  15.             Application.Run(new ShowProgressStatus());
  16.         }

  17.         public ShowProgressStatus()
  18.         {
  19.             InitializeComponent();
  20.         }

  21.         /// <summary>
  22.         ///    Required method for Designer support - do not modify
  23.         ///    the contents of this method with an editor
  24.         /// </summary>
  25.         private void InitializeComponent()
  26.         {
  27.             this.components = new System.ComponentModel.Container();
  28.             this.label1 = new System.Windows.Forms.Label();
  29.             this.progressBar1 = new System.Windows.Forms.ProgressBar();
  30.             this.btnProcess = new System.Windows.Forms.Button();
  31.             this.textBox1 = new System.Windows.Forms.TextBox();
  32.             this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
  33.               
  34.             label1.Location = new System.Drawing.Point(32, 40);
  35.             label1.Text = "Progress Value";
  36.             label1.Size = new System.Drawing.Size(88, 24);
  37.             label1.TabIndex = 2;
  38.               
  39.             progressBar1.Maximum = 10;
  40.             progressBar1.Location = new System.Drawing.Point(8, 312);
  41.             progressBar1.Minimum = 0;
  42.             progressBar1.TabIndex = 0;
  43.             progressBar1.Value = 0;
  44.       
  45.             //We have calculated the excat size which will result in only 20 boxes to be drawn
  46.               
  47.             progressBar1.Size = new System.Drawing.Size(520, 40);
  48.             progressBar1.Step = 1;
  49.               
  50.             btnProcess.Location = new System.Drawing.Point(152, 168);
  51.             btnProcess.Size = new System.Drawing.Size(144, 48);
  52.             btnProcess.TabIndex = 1;
  53.             btnProcess.Text = "Process";
  54.             btnProcess.Click += new System.EventHandler(btnProcess_Click);
  55.               
  56.             textBox1.Location = new System.Drawing.Point(136, 40);
  57.             textBox1.Text = "0";
  58.             textBox1.TabIndex = 3;
  59.             textBox1.Size = new System.Drawing.Size(184, 20);
  60.             this.Text = "Display Progress Status";
  61.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  62.             this.ClientSize = new System.Drawing.Size(616, 393);
  63.             this.StartPosition = FormStartPosition.CenterScreen;

  64.               
  65.             this.Controls.Add(textBox1);
  66.             this.Controls.Add(label1);
  67.             this.Controls.Add(btnProcess);
  68.             this.Controls.Add(progressBar1);

  69.             // To report progress from the background worker we need to set this property
  70.             backgroundWorker1.WorkerReportsProgress = true;

  71.             // This event will be raised on the worker thread when the worker starts
  72.             backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);

  73.             // This event will be raised when we call ReportProgress
  74.             backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
  75.          
  76.             backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
  77.             //当backgroundWorker做完后发生的事件
  78.         }

  79.         // On worker thread so do our thing!
  80.         void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  81.         {
  82.             isProcessRunning = true;
  83.             Thread.Sleep(100);
  84.             // Your background task goes here
  85.             for (int i = 0; i <= fileCount; i++)
  86.             {
  87.                 // Report progress to 'UI' thread
  88.                 backgroundWorker1.ReportProgress(i);
  89.                 // Simulate long task
  90.                 System.Threading.Thread.Sleep(200);
  91.                 textBox1.BeginInvoke(
  92.                     new Action(() =>
  93.                         {
  94.                             textBox1.Text = (i + 1).ToString();
  95.                         }
  96.                 ));
  97.             }
  98.         }

  99.         // Back on the 'UI' thread so we can update the progress bar
  100.         void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  101.         {
  102.             // The progress percentage is a property of e
  103.             progressBar1.Value = e.ProgressPercentage;
  104.         }

  105.         void btnProcess_Click(object sender, EventArgs e)
  106.         {
  107.             if (isProcessRunning)
  108.             {
  109.                 MessageBox.Show("A process is already running.");
  110.                 return;
  111.             }

  112.             string[] files = Directory.GetFiles(@"c:\", "*");
  113.             progressBar1.Maximum = files.Length;
  114.             fileCount = files.Length;

  115.             // Start the background worker
  116.             backgroundWorker1.RunWorkerAsync();
  117.         }

  118.         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  119.         {
  120.             //如果数组中有东西,那么加入ComboBox
  121.             progressBar1.Value = progressBar1.Minimum;
  122.             isProcessRunning = false;
  123.         }

  124.         private System.Windows.Forms.Button btnProcess;
  125.         private System.ComponentModel.Container components;
  126.         private System.Windows.Forms.TextBox textBox1;
  127.         private System.Windows.Forms.Label label1;
  128.         private System.Windows.Forms.ProgressBar progressBar1;
  129.         private System.ComponentModel.BackgroundWorker backgroundWorker1;
  130.         private bool isProcessRunning;
  131.         private int fileCount;
  132.     }
  133. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马