(you must avoid using threads as much as possible because they consume a lot of memory and they require time to create, destroy,and manage . Time is also wasted when Windows context switches between threads and when garbage collections occur . However, this discussion should also help you realize that threads must be used sometimes because they allow Windows to be robust and responsive)摘自 CLR via C#
1
namespace
WpfThreadTest
2
{
3
///
<summary>
4
///
ThreadPoolDemo.xaml 的交互逻辑
5
///
</summary>
6
public
partial
class
ThreadPoolDemo : Window
7
{
8
public
ThreadPoolDemo()
9
{
10
InitializeComponent();
11
///
设置线程池的最大可用辅助线程数目,最大异步线程数目
12
ThreadPool.SetMaxThreads(
100
,
10
);
13
14
}
15
16
private
void
button1_Click(
object
sender, RoutedEventArgs e)
17
{
18
//
调用线程去计算数字,仅供演示
19
ThreadPool.QueueUserWorkItem(
new
WaitCallback(CountNumber),
1
);
20
}
21
private
void
CountNumber(
object
state)
22
{
23
int
count
=
int
.Parse(state.ToString());
24
while
(
true
)
25
{
26
count
++
;
27
this
.textBlock2.Dispatcher.Invoke(
28
DispatcherPriority.Normal,
29
new
Action(()
=>
30
{
31
this
.textBlock2.Text
=
count.ToString();
32
}));
33
Thread.Sleep(
100
);
34
}
35
}
36
///
<summary>
37
///
测试线程中传递引用对象,以期望获得返回值
38
///
</summary>
39
///
<param name="state"></param>
40
private
void
ReturnValueMethod(
object
state)
41
{
42
MyParam myParam
=
state
as
MyParam;
43
myParam.MyInt
=
100
;
44
myParam.MyString
=
"
updated string
"
;
45
}
46
47
private
void
button2_Click(
object
sender, RoutedEventArgs e)
48
{
49
///
将引用对象传递给线程
50
MyParam myParam
=
new
MyParam() { MyInt
=
12
, MyString
=
"
test
"
};
51
52
ThreadPool.QueueUserWorkItem(
new
WaitCallback(ReturnValueMethod), myParam);
53
54
///
主线程睡眠5ms后,再去读取对象的值
55
///
如果不睡眠,直接读取,可能读不到修改后的值,因为他们处于不同的线程当中
56
///
当主线程去读取时,子线程尚未更改,
57
///
即便如此,也会有读取不到的时候,故此种传值方式并不十分可取,
58
///
具体线程返回值可以参考MSDN
http://msdn.microsoft.com/zh-cn/library/wkays279.aspx
59
Thread.Sleep(
5
);
60
this
.textBlock1.Text
=
string
.Format(
"
MyInt:{0},MyString:{1}
"
, myParam.MyInt, myParam.MyString);
61
}
62
63
private
void
button3_Click(
object
sender, RoutedEventArgs e)
64
{
65
ThreadPool.QueueUserWorkItem(
new
WaitCallback(GetThreadPoolInfo),
null
);
66
}
67
private
void
GetThreadPoolInfo(
object
state)
68
{
69
while
(
true
)
70
{
71
int
workerThreads
=
0
;
72
int
completionPortThreads
=
0
;
73
ThreadPool.GetAvailableThreads(
out
workerThreads,
out
completionPortThreads);
74
this
.textBlock3.Dispatcher.Invoke(DispatcherPriority.SystemIdle,
new
Action(()
=>
75
{
76
this
.textBlock3.Text
=
77
string
.Format(
"
池中可用辅助线程:{0},可用异步 I/O 线程:{1}
"
,
78
workerThreads, completionPortThreads);
79
}));
80
Thread.Sleep(
1000
);
81
}
82
}
83
}
84
public
class
MyParam
85
{
86
public
int
MyInt {
get
;
set
; }
87
public
string
MyString {
get
;
set
; }
88
}
89
}