zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    数组、链表、Hash的优缺点
    数据库-索引的坏处,事务的级别,分布式事务的原理。
    4G内存的电脑,如何读取8G的日志文件进行分析,汇总数据成报表的面试题
    数据库常用的锁有哪些
    2020年最新 C# .net 面试题,月薪20K+中高级/架构师必看(十)
    ThreadX应用开发笔记之一:移植ThreadX到STM32平台
    net core 方法 返回值 重改?
    使用RestTemplate发送HTTP请求举例
    dedecms织梦手机站上一篇下一篇链接错误的解决方法
    多目标跟踪之数据关联(匈牙利匹配算法和KM算法)
  • 原文地址:https://www.cnblogs.com/FindSelf/p/3586008.html
Copyright © 2011-2022 走看看