AsyncCallback The Windows Control
在项目中经常有装入大批量数据,需要显示进度。
但Windows Control不是线程安全的。
在装库过程中,可能要对Control进行操作,此时,如果随意拖动窗体。
主进程就无法进行对Control的Redraw.
所以就要采用异步的方法。
1
private SortedList sl = null;
2
private const int TEST_COUNT = 10000;
3
private void button1_Click(object sender, System.EventArgs e)
4
{
5
mi = new MethodInvoker(DoWork);
6
mi.BeginInvoke(new AsyncCallback(DoneWork), null);
7
}
8
private void DoWork()
9
{
10
sl = new SortedList();
11
InitialPB(TEST_COUNT);
12
for (int i =0;i<TEST_COUNT;i++)
13
{
14
sl.Add(i,"NoWay" + i.ToString());
15
Thread.Sleep(100);
16
RefreshText("NoWay" + i.ToString());
17
RefreshPB(1);
18
}
19
20
}
21
private void InitialPB(int max)
22
{
23
if( InvokeRequired )
24
{
25
this.Invoke( new IntDelegate(InitialPB), new object [] { max } );
26
return ;
27
}
28
progressBar1.Maximum = max ;
29
progressBar1.Value = 0;
30
}
31
private void RefreshText(string item)
32
{
33
if( InvokeRequired )
34
{
35
this.Invoke( new StringDelegate(RefreshText), new object [] { item } );
36
return ;
37
}
38
this.textBox1.Text = item;
39
}
40
private void RefreshPB(int Value)
41
{
42
if( InvokeRequired )
43
{
44
this.Invoke( new IntDelegate(RefreshPB), new object [] {Value} );
45
return ;
46
}
47
progressBar1.Value+=Value;
48
}
49
private void DoneWork(IAsyncResult result)
50
{
51
if( InvokeRequired )
52
{
53
Invoke(new AsyncCallback(DoneWork), new Object [] { result } );
54
return ;
55
}
56
MessageBox.Show("Done!")
57
;
58
}
59
private void button2_Click(object sender, System.EventArgs e)
60
{
61
mi.EndInvoke(null);
62
}
63
private MethodInvoker mi= null;
64
private delegate void IntDelegate(int num);
65
private delegate void StringDelegate(string str);
66
private SortedList sl = null;2
private const int TEST_COUNT = 10000;3
private void button1_Click(object sender, System.EventArgs e)4
{5
mi = new MethodInvoker(DoWork);6
mi.BeginInvoke(new AsyncCallback(DoneWork), null);7
}8
private void DoWork()9
{10
sl = new SortedList();11
InitialPB(TEST_COUNT);12
for (int i =0;i<TEST_COUNT;i++)13
{14
sl.Add(i,"NoWay" + i.ToString());15
Thread.Sleep(100);16
RefreshText("NoWay" + i.ToString());17
RefreshPB(1);18
}19
20
}21
private void InitialPB(int max)22
{23
if( InvokeRequired )24
{25
this.Invoke( new IntDelegate(InitialPB), new object [] { max } );26
return ;27
}28
progressBar1.Maximum = max ;29
progressBar1.Value = 0;30
}31
private void RefreshText(string item)32
{33
if( InvokeRequired )34
{35
this.Invoke( new StringDelegate(RefreshText), new object [] { item } );36
return ;37
}38
this.textBox1.Text = item;39
}40
private void RefreshPB(int Value)41
{42
if( InvokeRequired )43
{44
this.Invoke( new IntDelegate(RefreshPB), new object [] {Value} );45
return ;46
}47
progressBar1.Value+=Value;48
}49
private void DoneWork(IAsyncResult result)50
{51
if( InvokeRequired )52
{53
Invoke(new AsyncCallback(DoneWork), new Object [] { result } );54
return ;55
}56
MessageBox.Show("Done!")57
;58
}59
private void button2_Click(object sender, System.EventArgs e)60
{61
mi.EndInvoke(null);62
}63
private MethodInvoker mi= null;64
private delegate void IntDelegate(int num);65
private delegate void StringDelegate(string str);66



浙公网安备 33010602011771号