试试这个 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 WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; RegisterEvent(); } public delegate void MyEventHandler(object obj, int e); public event MyEventHandler OnNumberAdd; public void RegisterEvent() { OnNumberAdd += new MyEventHandler(Form1_OnNumberAdd); } void Form1_OnNumberAdd(object obj, int e) { this.button1.Text = e.ToString();//button 换成ProgressBar } Thread t = null; public void CreateBatch() { t = new Thread(new ThreadStart(createbatch)); t.Start(); } public void createbatch() { //这里可设置成读取的数量 for (int i = 0; i < 100000; i++) { work(i, OnNumberAdd); } } private void work(int e, MyEventHandler temp) { if (OnNumberAdd != null) { foreach (MyEventHandler handler in temp.GetInvocationList()) { handler.BeginInvoke(this, e, null, null); } } } private void button1_Click(object sender, EventArgs e) { CreateBatch(); } } }
2、灵活控制的话,用线程:
Thread,ProgressBar.InvokeRequired,ProgressBar.Invoke(....);
就满足了
有我的一段代码,有兴趣的话可以去看看,大同小异
private bool _StopRequest;
private object _SycObj=new Object();
private void SetPro()
{
if (Monitor.TryEnter(_SycObj, 500))
{
while(!_StopRequest)
{
Thread.Sleep(1000);
this.Invoke(new Do(IncProValue));
}
}
}private void IncProValue()
{
if(ProgressBar1.value>=ProgressBar1.MaxValue@?)
{
ProgressBar1.MaxValue@?*=0.1;
}
ProgressBar1.value++;
}private void Start()
{
_StopRequest=false;
Thread t=new Thread(new ThreadStart(SetPro));
t.IsBackGroud=true;
t.Start();
}
private void Stop()
{
_StopRequest=true;
}没试过看可以不。
progressBar1.最大值 = 100;
progressBar1.value+=1;
就可以了啊。
public delegate void Do();
private bool _StopRequest;
private object _SycObj = new Object(); private void SetPro()
{
if (Monitor.TryEnter(_SycObj, 500))
{
try
{
while (!_StopRequest)
{
Thread.Sleep(100);
this.Invoke(new Do(IncProValue));
}
}
finally
{
Monitor.Exit(_SycObj);
} }
} private void IncProValue()
{
if (progressBar1.Value >= progressBar1.Maximum)
{
progressBar1.Value = 0;
}
progressBar1.Value++;
} private void Start()
{
_StopRequest = false;
Thread t = new Thread(new ThreadStart(SetPro));
t.IsBackground = true;
t.Start();
}
private void Stop()
{
_StopRequest = true;
} public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Start();
} private void button2_Click(object sender, EventArgs e)
{
Stop();
}
jf快樂!
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
RegisterEvent();
} public delegate void MyEventHandler(object obj, int e); public event MyEventHandler OnNumberAdd; public void RegisterEvent()
{
OnNumberAdd += new MyEventHandler(Form1_OnNumberAdd);
} void Form1_OnNumberAdd(object obj, int e)
{
this.button1.Text = e.ToString();//button 换成ProgressBar
} Thread t = null; public void CreateBatch()
{
t = new Thread(new ThreadStart(createbatch));
t.Start();
} public void createbatch()
{
//这里可设置成读取的数量
for (int i = 0; i < 100000; i++)
{
work(i, OnNumberAdd);
}
}
private void work(int e, MyEventHandler temp)
{
if (OnNumberAdd != null)
{
foreach (MyEventHandler handler in temp.GetInvocationList())
{
handler.BeginInvoke(this, e, null, null);
}
}
} private void button1_Click(object sender, EventArgs e)
{
CreateBatch();
}
}
}