在类中自定义事件.
并在类的方法中根据实际需要循环触发该事件.
用debug跟踪 ,结果发现:
循环触发的事件的次数=(2+循环次数) * 循环次数 /2
即循环时,第一次触发1次
二 2
. .
. .
. .
n n不知道如何解决该问题.何向各个大侠求救.
代码如下
自定义类
class Class1
{
public Class1()
{
}
public delegate void test(int index,int count);
public event test testEvent;
public void Client()
{
//循环触发事件,测试用
for (int i = 0; i <= 10; i++)
{
testEvent(i+1,10);
}
}
}
-------------------------------------
调用该类的窗体
public partial class Form1 : Form
{
private Class1 cls;
public Form1()
{
InitializeComponent();
cls = new Class1();
cls.testEvent += new Class1.test(cls_testEvent);
} void cls_testEvent(int index, int count)
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Trace.AutoFlush = true;
Trace.WriteLine(index.ToString() + " " + count.ToString()); this.progressBar1.Value = (int)(index / count * 100); } private void button1_Click(object sender, EventArgs e)
{
cls.Client();
}
}
并在类的方法中根据实际需要循环触发该事件.
用debug跟踪 ,结果发现:
循环触发的事件的次数=(2+循环次数) * 循环次数 /2
即循环时,第一次触发1次
二 2
. .
. .
. .
n n不知道如何解决该问题.何向各个大侠求救.
代码如下
自定义类
class Class1
{
public Class1()
{
}
public delegate void test(int index,int count);
public event test testEvent;
public void Client()
{
//循环触发事件,测试用
for (int i = 0; i <= 10; i++)
{
testEvent(i+1,10);
}
}
}
-------------------------------------
调用该类的窗体
public partial class Form1 : Form
{
private Class1 cls;
public Form1()
{
InitializeComponent();
cls = new Class1();
cls.testEvent += new Class1.test(cls_testEvent);
} void cls_testEvent(int index, int count)
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Trace.AutoFlush = true;
Trace.WriteLine(index.ToString() + " " + count.ToString()); this.progressBar1.Value = (int)(index / count * 100); } private void button1_Click(object sender, EventArgs e)
{
cls.Client();
}
}
循环触发在实际中的运用是
比较说,我要处理100万条记录.
为了直观,我给界面做一个进度条.
处理这100成条记录的方法中自定义类中,该方法在处理完一条记录后,触发事件testEvent,并将当前处理记录的索引值index,以及总记录数count传出去.
在客记端处理该事件时,根据index,count可以算出当前处理所占的百分比.并反映到进度条中.希望各位高手,大侠来此为小弟解惑.小弟不胜感激!!!!!!!!!
最初是由于进度条根本无法正确显示.
可能是每条数据处理的时间太短导致.
用System.Threading.Thread.Sleep(1000);在循环中暂停.也无法正常显示.
进度条只在循环完毕后才显示百分之百我用
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Trace.AutoFlush = true;
Trace.WriteLine(index.ToString() + " " + count.ToString());
来跟踪传过来的值.
如下
1 10
2 10
2 10
3 10
3 10
3 10
4 10
4 10
4 10
4 10
5 10
5 10
5 10
5 10
5 10
6 10
6 10
6 10
6 10
6 10
6 10
7 10
7 10
7 10
7 10
7 10
7 10
7 10
8 10
8 10
8 10
8 10
8 10
8 10
8 10
8 10
9 10
9 10
9 10
9 10
9 10
9 10
9 10
9 10
9 10
10 10
10 10
10 10
10 10
10 10
10 10
10 10
10 10
10 10
10 10
11 10
11 10
11 10
11 10
11 10
11 10
11 10
11 10
11 10
11 10
11 10
为什么进度条无法准确地显示进度呢?
明明index,count传过来的是正确的值.根据Trace跟踪的结果,可知虽然理论上只循环了11次,但是实际上客户端处理的其触发的事件
有(1+11)*11/2次.如果真的循环100万次,那可就郁闷了.
为什么会出现这种情况?
请各路高手出手相助.小北感激涕零!!!!!!!!!!!1
将
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Trace.AutoFlush = true;
放入load事件中,或者初始化方法中.返回的结果就正常了.现在的问题只剩下一点.
为什么进度条没有正常的显示?????????????????????
请各位大侠指点!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
当我用线程来单独调用处理100万条记录的方法时.
出现错误,事件处理方法中的
this.progressBar1.Value = (int)(index / count * 100);
提示是跨越了线程.
应该是处理事件是在主线程.
与处理100万数据的线程不同.我想进度条无法显示的原因,很可能是由于数据处理得太快
以致界面进入一种失去响应的状态.
界面无法重绘.只有在处理结束时才恢复过来.不知道如果做到正确的显示进度条呢?
请高手赐教!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
先谢谢了.
让界面重绘。
建立界面线程,专门更新界面
工作线程把当前的进度写入到一个全局变量,
然后UI线程一直读这个变量,
并显示进度条。我想yudith(yudith)的话应该这样理解.
谢谢了,小弟试一试.