在不同线程之间传递数据
一般情况下,在线程间是不能交换数据的,不过在相同应用程序域中的线程则可以共享应用程序域的数据。我们可以通过AppDomain的GetData和SetData方法来实现这一功能。具体见源代码。
1
using System;
2
using System.Threading;
3![](/Images/OutliningIndicators/None.gif)
4
namespace ConsoleDemo
5
{
6
/// <summary>
7
/// Class1 的摘要说明。
8
/// </summary>
9
class Class1
10
{
11
/// <summary>
12
/// 应用程序的主入口点。
13
/// </summary>
14
[STAThread]
15
static void Main(string[] args)
16
{
17
//
18
// TODO: 在此处添加代码以启动应用程序
19
//
20
int inputParam = 10;
21
22
Thread demoTd = new Thread(new ThreadStart(Run));
23
demoTd.IsBackground = true;
24![](/Images/OutliningIndicators/InBlock.gif)
25
Thread.GetDomain().SetData("demo", inputParam); //设置应用程序域的数据槽的数据
26
demoTd.Start();
27
Console.Read();
28
}
29![](/Images/OutliningIndicators/InBlock.gif)
30
static void Run()
31
{
32
int tmp = 0;
33
Console.WriteLine(tmp);
34
try
35
{
36
tmp = Convert.ToInt32(Thread.GetDomain().GetData("demo")); //读取应用程序域中的数据槽数据
37
}
38
catch(Exception exp)
39
{
40
Console.WriteLine(exp);
41
Console.Read();
42
}
43
Console.WriteLine(tmp);
44
Console.Read();
45
}
46
}
47
}
48![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
48
![](/Images/OutliningIndicators/None.gif)