zoukankan      html  css  js  c++  java
  • 多线程显示运行状态

    最近碰见一个例子,Copy大文件或者网络访问的时候处理假死。 那就用多线程加个进度条(只显示运行,没有进度)来表示状态运行吧。好了,废话少说,上个例子。先看结果图:

    image image

    程序说明:

    点击Button,运行一个数据累加器,textBox显示每次运行的结果,ProgressBar表示运行的状态。

    好了,直接贴代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace Testpro
    {
        public partial class Form1 : Form
        {
            BackgroundWorker work = new BackgroundWorker();
            public Form1()
            {
                InitializeComponent();
                work.WorkerReportsProgress = true;
                work.DoWork += Count;
                work.RunWorkerCompleted += completeRun;
                Control.CheckForIllegalCrossThreadCalls = false;
                this.textBox1.ScrollBars = ScrollBars.Both;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.progressBar1.Style = ProgressBarStyle.Marquee;
                
                work.RunWorkerAsync();
            }
    
            private void Count(object sender, DoWorkEventArgs e)
            {            
                float num = 0.0f;
                int cun = 0;
                while (num < 5000)
                {
                    cun++;
                    num += 4f;              
                   this.textBox1.Text += num.ToString() + "\t" + e.Result;
                   if (cun == 9)
                   {
                       textBox1.Text += Environment.NewLine;
                       cun = 0;
                   }
                }            
            }
    
            public  void SetText(object num)
            {
                textBox1.Text += num.ToString() + "\n";
            }
    
            private void completeRun(object sender,  RunWorkerCompletedEventArgs e)
            {
                this.progressBar1.Style = ProgressBarStyle.Blocks;
                
                this.textBox1.Text += "complete";
                MessageBox.Show("Running complete.....");
            }       
        }
    }
    
    

  • 相关阅读:
    linux shell 脚本显示执行的命令与关闭显示执行的命令
    docker-compose 文件参考
    django 关系字段一对多的筛选
    深度学习-tensorflow学习笔记(2)-MNIST手写字体识别
    opencv-学习笔记(6)图像梯度Sobel以及canny边缘检测
    opencv-学习笔记(5)形态学转变
    opencv-学习笔记(4)-模糊
    opencv-学习笔记(3)
    opencv-学习笔记(2)
    opencv-学习笔记(1)常用函数和方法。
  • 原文地址:https://www.cnblogs.com/jimson/p/ThreadBar.html
Copyright © 2011-2022 走看看