• 大小: 45KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-20
  • 语言: C#
  • 标签: 进度条  

资源简介

C#使用委托和事件写的,一个多线程进度条的Demo。

资源截图

代码片段和文件信息

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 MultiThreadProcess_Test
{
    public partial class Form1 : Form
    {        
        protected Thread thread1 = null;//开启新的线程(用来处理数据)
        public static bool isStop = false;//控制线程的暂停和继续的标记
        public AutoResetEvent autoResetEvent = new AutoResetEvent(false);//控制线程的暂停和继续

        private delegate void MessageEventHandler(object sender ProgressBarValueEventArgs e);
        private event MessageEventHandler messageEvent;

        private delegate void RefreshButtonEventHandler();
        private event RefreshButtonEventHandler refreshButtonEvent;

        public Form1()
        {
            InitializeComponent();

            messageEvent += new MessageEventHandler(PassInfo);//第一种方式
            //messageEvent += new MessageEventHandler(ShowProcessing);//第二种方式(同刷新按钮)

            refreshButtonEvent += new RefreshButtonEventHandler(RefreshButton);

            btnStart.Enabled = true;
            btnStop.Enabled = false;
            btnContinue.Enabled = false;
            btnOver.Enabled = false;
        }

        public void Thread_DataProcessing()
        {
            ProgressBarValueEventArgs progressbarValueEventArgs = new ProgressBarValueEventArgs(0 “开始处理...“);
            ShowProcessing(this progressbarValueEventArgs);//进度提示
            int intCount = 100;
            for (int i = 0; i < intCount; i++)//这个循环可以换成处理自己的数据
            {
                progressbarValueEventArgs = new ProgressBarValueEventArgs((int)((i + 1) * 100 / intCount) string.Format(“已处理{0}/{1}个数据“ (i + 1) intCount));
                ShowProcessing(this progressbarValueEventArgs);
                System.Threading.Thread.Sleep(50);
                if (isStop)
                    autoResetEvent.WaitOne();//线程暂停
            }
            progressbarValueEventArgs = new ProgressBarValueEventArgs(100 “数据处理已完成“);
            ShowProcessing(this progressbarValueEventArgs);
            refreshButtonEvent();//刷新按钮
        }

        public void ShowProcessing(object sender ProgressBarValueEventArgs e)
        {
            this.Invoke(this.messageEvent new object[] { sender e });
            //this.BeginInvoke(this.messageDelegate new object[] { sender e });

            //if (this.InvokeRequired)
            //{
            //    this.Invoke(this.messageEvent new object[] { sender e });
            //}
            //else
            //{
            //    this.lblProgressInfo.Text = string.Format(“{0}  {1}%“ e.DescriptionStr e.Process);
            //    this.progressBar1.Value = e.Process;
            //}
        }

        private void PassInfo(object sender ProgressBarValueEventArgs e)
        {
            if (thread1 == null)
            {
                this.

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        911  2016-09-18 09:14  MultiThreadProcess_Test\MultiThreadProcess_Test.sln

    ..A..H.     22016  2016-09-18 09:45  MultiThreadProcess_Test\MultiThreadProcess_Test.suo

     文件       6579  2016-09-18 09:43  MultiThreadProcess_Test\MultiThreadProcess_Test\Form1.cs

     文件       5236  2016-09-18 09:28  MultiThreadProcess_Test\MultiThreadProcess_Test\Form1.Designer.cs

     文件       5817  2016-09-14 09:33  MultiThreadProcess_Test\MultiThreadProcess_Test\Form1.resx

     文件       3761  2016-09-18 09:20  MultiThreadProcess_Test\MultiThreadProcess_Test\MultiThreadProcess_Test.csproj

     文件        504  2016-09-18 09:14  MultiThreadProcess_Test\MultiThreadProcess_Test\Program.cs

     文件        680  2016-09-18 09:21  MultiThreadProcess_Test\MultiThreadProcess_Test\ProgressBarValueEventArgs.cs

     文件      12800  2016-09-18 09:43  MultiThreadProcess_Test\MultiThreadProcess_Test\bin\Debug\MultiThreadProcess_Test.exe

     文件      26112  2016-09-18 09:43  MultiThreadProcess_Test\MultiThreadProcess_Test\bin\Debug\MultiThreadProcess_Test.pdb

     文件      11600  2016-09-18 16:25  MultiThreadProcess_Test\MultiThreadProcess_Test\bin\Debug\MultiThreadProcess_Test.vshost.exe

     文件       2855  2016-09-18 09:14  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6407  2016-09-18 09:43  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1051  2016-09-18 16:25  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86\Debug\MultiThreadProcess_Test.csproj.FileListAbsolute.txt

     文件        975  2016-09-18 09:30  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86\Debug\MultiThreadProcess_Test.csproj.GenerateResource.Cache

     文件      12800  2016-09-18 09:43  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86\Debug\MultiThreadProcess_Test.exe

     文件        180  2016-09-18 09:30  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86\Debug\MultiThreadProcess_Test.Form1.resources

     文件      26112  2016-09-18 09:43  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86\Debug\MultiThreadProcess_Test.pdb

     文件        180  2016-09-18 09:30  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86\Debug\MultiThreadProcess_Test.Properties.Resources.resources

     文件       1378  2016-09-18 09:14  MultiThreadProcess_Test\MultiThreadProcess_Test\Properties\AssemblyInfo.cs

     文件       2898  2016-09-18 09:14  MultiThreadProcess_Test\MultiThreadProcess_Test\Properties\Resources.Designer.cs

     文件       5612  2016-09-18 09:14  MultiThreadProcess_Test\MultiThreadProcess_Test\Properties\Resources.resx

     文件       1110  2016-09-18 09:14  MultiThreadProcess_Test\MultiThreadProcess_Test\Properties\Settings.Designer.cs

     文件        249  2016-09-18 09:14  MultiThreadProcess_Test\MultiThreadProcess_Test\Properties\Settings.settings

     目录          0  2016-09-18 09:14  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86\Debug\TempPE

     目录          0  2016-09-18 09:43  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86\Debug

     目录          0  2016-09-18 09:30  MultiThreadProcess_Test\MultiThreadProcess_Test\bin\Debug

     目录          0  2016-09-18 09:18  MultiThreadProcess_Test\MultiThreadProcess_Test\bin\Release

     目录          0  2016-09-18 09:14  MultiThreadProcess_Test\MultiThreadProcess_Test\obj\x86

     目录          0  2016-09-18 09:18  MultiThreadProcess_Test\MultiThreadProcess_Test\bin

............此处省略7个文件信息

评论

共有 条评论