首页 | 互联网 | IT动态 | Cisco | Windows | Linux | Java | .Net | Oracle | 华为 | 存储世界 | 服务器 | 网络设备 | IDC | 安全 | 求职招聘
IT培训 | 数字网校 | 技术专题 | 电子书下载 | 教学视频 | 网页设计 | 平面设计 | 解决方案 | 直播室 | 虚拟考场 | 搜索 | 博客 | 沙龙 | 论坛
中国IT实验室Dotnet频道
中国IT教育
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 文章正文

c#中子线程控制进度条的一个简单例子

文章来源中国IT实验室收集整理 作者佚名 更新时间2008-5-8 保存本文保存本文 推荐给好友推荐给好友 收藏本页收藏本页

    这个问题来自社区提问,代码保留一份用来以后回答

   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);
                }
            }
         }
    }

 

【责编:Ken】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题
 ·展现C#世界 C#程序设计专题
 ·Java入门 Tomcat的配置技巧精华专题
 ·Oracle RMAN物理备份技术详解
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航