• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

gisoracle

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

C# 多线程更新Processbar

C# 多线程更新Processbar

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

namespace temp
{
    delegate void SetValueCallback(int value);

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(Foo));

            t.Start();


        }
        private void Foo()
        {

            for (int i = 1; i <= 100; i++)
            {

                Thread.Sleep(100);

                SetProcessBarValue(i);

                SetLabelValue(i);

            }

        }

        private void SetLabelValue(int value)
        {

            // InvokeRequired required compares the thread ID of the

            // calling thread to the thread ID of the creating thread.

            // If these threads are different, it returns true.

            if (this.label1.InvokeRequired)
            {

                SetValueCallback d = new SetValueCallback(SetLabelValue);

                this.Invoke(d, new object[] { value });

            }

            else
            {

                this.label1.Text = value.ToString() + '%';

            }

        }

        private void SetProcessBarValue(int value)
        {

            // InvokeRequired required compares the thread ID of the

            // calling thread to the thread ID of the creating thread.

            // If these threads are different, it returns true.

            if (this.progressBar1.InvokeRequired)
            {

                SetValueCallback d = new SetValueCallback(SetProcessBarValue);

                this.Invoke(d, new object[] { value });

            }

            else
            {

                this.progressBar1.Value = value;

            }

        }

 


    }
}

posted on 2010-02-20 15:42  gisai  阅读(2537)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3