博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#滚动条创建实例,多线程
阅读量:4302 次
发布时间:2019-05-27

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

1.滚动条

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1

{
    public partial class MyProgressBar : Form
    {
        public MyProgressBar()
        {
            InitializeComponent();
            Initial();
        }
        public static int maxValue = 0;
        public static int minValue = 0;

        public void Initial()

        {

            this.progressBar1.Maximum = 100;

            this.progressBar1.Minimum = 0;

            maxValue = 100;

            minValue = 0;

        }

        //public delegate void UpdateValue(int value);
        public bool UpdateValue(int nValue)
        {
            while (!this.IsHandleCreated)
            {
                //MessageBox.Show("等待窗体句柄创建");
            };
            //try
            //{
                this.Invoke(new Action(() =>
                {
                    //if (this.Handle == null) return ;
                    if (nValue > 0)
                    {
                        this.label1.Text = nValue.ToString()+"%";
                        if ( nValue < progressBar1.Maximum)
                        {
                            progressBar1.Value =nValue;

                        }

                        else
                        {
                            progressBar1.Value = progressBar1.Maximum;
                            this.Close();

                        }

                    }

                }));

                return true;
            //}
            //catch(Exception ee)
            //{
                //MessageBox.Show(ee.ToString());
                return true;
            //}
        }

        

    }

}
2.主窗体调用

 public delegate bool UpdateProgressValue(int value);

        public event UpdateProgressValue UpdateProgressEvent;
        public void CreatedataLineFile(string folderPath)
        {

            //创建进度条

            Thread th = new Thread(() =>
            {

                

                MyProgressBar pb = new MyProgressBar();

                pb.StartPosition = FormStartPosition.CenterScreen;
                UpdateProgressEvent += pb.UpdateValue;
                pb.ShowDialog();
            });
            th.Start();

            while (UpdateProgressEvent == null) ;

            for (int i = 0; i < 1000;i++ )
            {
                Thread.Sleep(10);
                UpdateProgressEvent(Convert.ToInt32(i * 1.0 / 1000 * (MyProgressBar.maxValue - MyProgressBar.minValue)+MyProgressBar.minValue));
            }
               
            return;

}

转载地址:http://hzlws.baihongyu.com/

你可能感兴趣的文章
java 流使用
查看>>
java 用流收集数据
查看>>
java并行流
查看>>
CompletableFuture 组合式异步编程
查看>>
mysql查询某一个字段是否包含中文字符
查看>>
Java中equals和==的区别
查看>>
JVM内存管理及GC机制
查看>>
Java:按值传递还是按引用传递详细解说
查看>>
Java中Synchronized的用法
查看>>
阻塞队列
查看>>
linux的基础知识
查看>>
接口技术原理
查看>>
五大串口的基本原理
查看>>
PCB设计技巧与注意事项
查看>>
linux进程之间通讯常用信号
查看>>
main函数带参数
查看>>
PCB布线技巧
查看>>
关于PCB设计中过孔能否打在焊盘上的两种观点
查看>>
PCB反推理念
查看>>
京东技术架构(一)构建亿级前端读服务
查看>>