Hello Mat

 找回密码
 立即注册
查看: 7564|回复: 1

C#使用线程实现进度条progressBar1

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-11-10 22:21:25 | 显示全部楼层 |阅读模式
C#使用线程实现进度条progressBar1:
使用环境:VS2013+Win7+32bit
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;

  11. namespace WindowsFormsApplication1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.         }

  19.         private delegate void SetPos(int ipos);//代理

  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             Thread fThread = new Thread(new ThreadStart(SleepT));
  23.             fThread.Start();
  24.         }

  25.         private void SleepT()
  26.         {
  27.             for (int i = 0; i < 500; i++)
  28.             {
  29.                 System.Threading.Thread.Sleep(10);
  30.                 SetTextMesssage(100 * i / 500);
  31.             }
  32.         }

  33.         private void SetTextMesssage(int ipos)
  34.         {
  35.             if (this.InvokeRequired)
  36.             {
  37.                 SetPos setpos = new SetPos(SetTextMesssage);
  38.                 this.Invoke(setpos, new object[] { ipos });
  39.             }
  40.             else
  41.             {
  42.                 this.progressBar1.Value = Convert.ToInt32(ipos);
  43.             }
  44.         }

  45.     }
  46. }
复制代码

百度网盘:http://pan.baidu.com/s/1c2h3tW



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
 楼主| 发表于 2022-3-6 21:53:22 | 显示全部楼层
  1.         // 进度条托管
  2.         private delegate void ShowProgressDelegate(int totalStep, int currentStep);
  3.         ProgreeLight ProgreeLightForm = new ProgreeLight();
  4.         private void ShowProgress(int currentStep, int totalStep)
  5.         {
  6.             if (ProgreeLightForm == null)
  7.             {
  8.                 ProgreeLightForm = new ProgreeLight();
  9.             }
  10.             else
  11.             {
  12.                 if (ProgreeLightForm.IsDisposed) //若子窗体关闭 则打开新子窗体 并显示
  13.                 {
  14.                     ProgreeLightForm = new ProgreeLight();
  15.                 }
  16.             }
  17.             ProgreeLightForm.Show();
  18.             ProgreeLightForm.SetProgreeBar(currentStep, totalStep);
  19.         }

  20. 调用:
  21.                 ShowProgressDelegate showProgressBar = new ShowProgressDelegate(ShowProgress);
  22.                 this.Invoke(showProgressBar, new object[] { 0, RowsCount_ });
复制代码
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-26 10:21 , Processed in 0.248020 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表