zoukankan      html  css  js  c++  java
  • c#中子线程控制进度条的一个简单例子(多线程问题)

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

            }

        }
  • 相关阅读:
    笔试:一个逻辑题
    jmeter,学这些重点就可以了
    性能测试:通过一个案例(等待锁超时)告诉你,性能到底要不要熟悉业务逻辑?
    源码解读:webdriver client的原理 (面试自动化:如果你认为知道18种定位方式就算会自动化,那就太low了)
    测试必备:jmeter测试http协议接口的各种传参方式
    Vue笔记:封装 axios 为插件使用
    Vue笔记:使用 axios 发送请求
    Tomcat笔记:Tomcat的执行流程解析
    Git笔记:Git介绍和常用命令汇总
    Spring Boot使用Shiro实现登录授权认证
  • 原文地址:https://www.cnblogs.com/xsmhero/p/2693747.html
Copyright © 2011-2022 走看看