博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#中子线程控制进度条的一个简单例子(多线程问题)
阅读量:7034 次
发布时间:2019-06-28

本文共 1299 字,大约阅读时间需要 4 分钟。

 
using
 System;

using System.ComponentModel;

using
 System.Windows.Forms;
namespace
 WindowsApplication4
{
    
/// <summary>
    
/// gui 类
    
/// </summary>
    public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }
        
private void button1_Click(object sender, EventArgs e)
        
{
            
//用子线程工作
            new System.Threading.Thread(new System.Threading.ThreadStart(StartDownload)).Start();
        }
        
//开始下载
        public void StartDownload()
        
{
            Downloader downloader 
= new Downloader();
            downloader.onDownLoadProgress 
+= new Downloader.dDownloadProgress(downloader_onDownLoadProgress);
            downloader.Start();
        }
        
//同步更新ui
        void downloader_onDownLoadProgress(long total, long current)
        
{
            
if (this.InvokeRequired)
            
{
                
this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] { total, current });
            }
            
else
            
{
                
this.progressBar1.Maximum = (int)total;
                
this.progressBar1.Value = (int)current;
            }
        }
    }
    
/// <summary>
    
/// 下载类
    
/// </summary>
    public class Downloader
    
{
        
//委托
        public delegate void dDownloadProgress(long total,long current);
        
//事件
        public event dDownloadProgress onDownLoadProgress;
        
//开始模拟工作
        public void Start()
        
{
            
for (int i = 0; i < 100; i++)
            
{
                
if (onDownLoadProgress != null)
                    onDownLoadProgress(
100, i);
                System.Threading.Thread.Sleep(
100);
            }
        }
    }

转载于:https://www.cnblogs.com/xsmhero/archive/2012/09/19/2693747.html

你可能感兴趣的文章
Docker容器学习梳理--日常操作总结
查看>>
Error:could not open 'D:\program Files\java\jre6\lib\i386\jvm.cfg
查看>>
nginx绑定多个端口
查看>>
Linux下完整编译Apache+OpenSSL实现HTTPS
查看>>
citrix4.5无法进入发布程序界面The supplied credentials could not be validated
查看>>
dhcp服务器
查看>>
12cR2_AWR相关参数设置
查看>>
亿美软通荣获“2017中国大数据风控最具影响力企业奖”
查看>>
deep ocr中文汉字识别
查看>>
EVAL script numkeys key [key ...] arg [arg ...]
查看>>
linux VMware Tools两种安装方法
查看>>
企业想要低成本的实时通讯 有信源通讯说找他们就对了
查看>>
oracle数据库迁移步骤思路
查看>>
普通用户使用zabbix配置事件已经发送邮件
查看>>
PKI与证书服务简单应用
查看>>
巧用第三方开源工具trash-git放置linux误删除文件
查看>>
jira4.4.5下载与汉化
查看>>
mysqlsla安装与慢查询分析
查看>>
DPM2012的基本配置
查看>>
做旅游的就要有驴子精神
查看>>