首页 > 代码库 > 今天看到的关于邮件发送进度条的写法

今天看到的关于邮件发送进度条的写法

 1 using System;  2 using System.Collections.Generic;  3 using System.ComponentModel;  4 using System.Data;  5 using System.Drawing;  6 using System.Text;  7 using System.Windows.Forms;  8 using System.Threading;  9 10 namespace ThreadUpdateUI 11 { 12     public partial class Form2 : Form 13     { 14         public Form2() 15         { 16             InitializeComponent(); 17         } 18 19         private void button1_Click(object sender, EventArgs e) 20         { 21             try 22             { 23                 backgroundWorker1.RunWorkerAsync(1000); 24             } 25             catch (Exception es) 26             { 27 28                 MessageBox.Show(es.Message); 29             } 30         } 31 32         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 33         { 34             BackgroundWorker bk=sender as BackgroundWorker;//把serder转化为BackgroundWorker(后台操作) 35             int count = (int)(e.Argument);//从 DoWorkEventArgs 参数的 Argument 属性中提取该操作所需的所有参数.并保存起来. 36             e.Result = longtime(count, bk, e);//将计算的结果赋给 DoWorkEventArgs 的 Result 属性。 37         } 38         Random rd = new Random();39         long longtime(int pp,BackgroundWorker bk,DoWorkEventArgs e) 40         { 41             long time=0; 42             int i = 0; 43             while (i < pp) 44             { 45                 i++; 46                 //判断是否取消后台操作 47                 if (bk.CancellationPending) 48                 { 49                     e.Cancel = true; 50                     break; 51                 } 52                 else 53                 { 54                     time += i; 55                 } 56                 //完成率 57                 int view = (int)((float)i / pp * 100); 58                 bk.ReportProgress(view);//已完成后台操作的百分完成率,范围0%-100% 59                 System.Threading.Thread.Sleep(5);//sleep()里面的数字越少就越快 60             } 61             return time; 62         } 63 64         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 65         { 66             if (e.Cancelled) 67             { 68                 MessageBox.Show("取消"); 69             } 70             else 71             { 72                 MessageBox.Show(e.Result.ToString()); 73             } 74         } 75 76         private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 77         { 78             progressBar1.Value = http://www.mamicode.com/e.ProgressPercentage;//获取异步任务的进度百分比 79             label1.Text = e.ProgressPercentage.ToString();//显示出来 80         } 81 82         private void button2_Click(object sender, EventArgs e) 83         { 84             backgroundWorker1.CancelAsync(); 85         } 86 87     } 88 }

代码作者:Kiss_零@http://bbs.bccn.net/

 

先记录下来,回头继续分析