• WinForm---进度条的实现方法


    (转自:http://www.cnblogs.com/Sue_/articles/2024932.html)

      看了好几个WinForm程序了,发现他们对进度条的处理完全失去了进度条的作用。他们都是采用Timer来处理,在线程结束的时候,直接赋值进度条达到100%。和我以前做WebForm程序的时候完全不一样,做WebForm程序的时候,进度条是根据总体数据和每步执行后而计算和更新的。在看了这几个WinForm程序后,我在想:是否所有WinForm程序,在进度条的处理上都不能保证实时进度显示?

      其实用Timer来处理,不停的更新进度条只是程序作者偷懒的方法。当然这样的好处就是可以简单化处理进度条,代码量少,不易出错,调试方便。

      还有一种方法,就是可以及时更新进度条的数据的。那就是采用事件驱动机制,在子线程中监视复杂处理过程中的设定的事件,及时更新!直接看代码:

     1 using System;
     2 using System.ComponentModel;
     3 using System.Windows.Forms;
     4 namespace WindowsApplication1
     5 {
     6      /// <summary>
     7      /// Form1 类
     8      /// </summary>
     9      public partial class Form1 : Form
    10      {
    11          public Form1()
    12          {
    13              InitializeComponent();
    14          }
    15          private void button1_Click(object sender, EventArgs e)
    16          {
    17              //用子线程工作
    18              new System.Threading.Thread(new System.Threading.ThreadStart(StartDownload)).Start();
    19          }
    20          //开始下载
    21          public void StartDownload()
    22          {
    23              Downloader downloader = new Downloader();
    24              downloader.onDownLoadProgress += new Downloader.dDownloadProgress(downloader_onDownLoadProgress);
    25              downloader.Start();
    26          }
    27          //同步更新UI
    28          void downloader_onDownLoadProgress(long total, long current)
    29          {
    30              if (this.InvokeRequired)
    31              {
    32                  this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] { total, current });
    33              }
    34              else
    35              {
    36                  this.progressBar1.Maximum = (int)total;
    37                  this.progressBar1.Value = (int)current;
    38              }
    39          }
    40      }
    41 
    42      /// <summary>
    43      /// 下载类(您的复杂处理类)
    44      /// </summary>
    45      public class Downloader
    46      {
    47          //委托
    48          public delegate void dDownloadProgress(long total,long current);
    49          //事件
    50          public event dDownloadProgress onDownLoadProgress;
    51          //开始模拟工作
    52          public void Start()
    53          {
    54              for (int i = 0; i < 100; i++)
    55              {
    56                  if (onDownLoadProgress != null)
    57                      onDownLoadProgress(100, i);
    58                  System.Threading.Thread.Sleep(100);
    59              }
    60          }
    61      }
    62 }
  • 相关阅读:
    使用日历控件的一些体会(downmoon)
    带附加条件的NewID()用法
    微软的招聘哲学——做微软人的五大核心素质(摘自《微软360度》)
    彻底禁用fckEditor的上传功能(含防止Type漏洞问题)
    Remote Access Connection Manager 服务因下列错误而停止解决办法
    GridView如何更新批量数据和单条记录?
    .net1.1与.net2.0在加载ascx文件的控件时有所不同(Downmoon)
    牛羊吃草问题求解(downmoon)
    c#操作ecxel的一些资源(downmoon搜集)
    安装sql2008 enterprise (English正式版)图解
  • 原文地址:https://www.cnblogs.com/FindSelf/p/3586008.html
Copyright © 2020-2023  润新知