黑马程序员技术交流社区
标题:
线程
[打印本页]
作者:
霍世勇
时间:
2012-4-9 16:03
标题:
线程
线程是如何传值的?
作者:
逝去的谎言
时间:
2012-4-10 11:33
举个例子:static void Main()
{
Thread t=new Thread(Print);
t.Start("Hello from t!");
Print("Hello from the main thread!");
}
static void Print(Object messageObj)
{
string message=(string)messageObj;
Console.WriteLine(message);
}
输出:Hello from t!
Hello from the main thread!
也就是现实例化一个线程Thread,然后使用t.Start调用Print这个方法并传递Hello from t!这个参数,接着在调用一个Print()方法,实现执行线程,再执行Print(Hello from the main thread!
)方法
作者:
翁金鹏
时间:
2012-4-10 12:14
Thread t = parserThread.CreateThread(new parserThread.Start(BindData), (object)DateTime.Now.Year.ToString(), (object)DateTime.Now.Month.ToString());
t.Start();
//阻塞调用线程
t.Join(Timeout.Infinite);
/////
public class parserThread
{
//代理代参数的方法
public delegate void Start(object objYear, object objMonth);
//这个类用来解决ThreadStart只能代理无参数无返回值函数而设计。
private class Argument
{
public object objYear, objMonth;//用来保存条件,年和月
public Start s1;
//建立该方法是为了由ThreadStart来代理,
public void parse()
{
s1(objYear, objMonth);
}
}
//创建返回线程。
public static Thread CreateThread(Start s, Object objYear, object objMonth)
{
Argument arg = new Argument();
arg.objYear = objYear;
arg.objMonth = objMonth;
arg.s1 = s;
Thread t = new Thread(new ThreadStart(arg.parse));
return t;
}
}
引用的别人写的列子 希望对你有帮助
作者:
yoosou2012
时间:
2012-4-10 12:36
使用C#往线程里传参数的方法总结:
Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托。
Thread (ThreadStart) 初始化 Thread 类的新实例。
由 .NET Compact Framework 支持。
Thread (ParameterizedThreadStart, Int32) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托,并指定线程的最大堆栈大小。
Thread (ThreadStart, Int32) 初始化 Thread 类的新实例,指定线程的最大堆栈大小。
由 .NET Compact Framework 支持。
我们如果定义不带参数的线程,可以用ThreadStart,带一个参数的用ParameterizedThreadStart。带多个参数的用另外的方法,下面逐一讲述。
一、不带参数的
01 using System;
02 using System.Collections.Generic;
03 using System.Text;
04 using System.Threading;
05
06 namespace AAAAAA
07 {
08 class AAA
09 {
10 public static void Main()
11 {
12 Thread t = new Thread(new ThreadStart(A));
13 t.Start();
14
15 Console.Read();
16 }
17
18 private static void A()
19 {
20 Console.WriteLine("Method A!");
21 }
22 }
23 }
结果显示Method A!
二、带一个参数的
由于ParameterizedThreadStart要求参数类型必须为object,所以定义的方法B形参类型必须为object。
01 using System;
02 using System.Collections.Generic;
03 using System.Text;
04 using System.Threading;
05
06 namespace AAAAAA
07 {
08 class AAA
09 {
10 public static void Main()
11 {
12 Thread t = new Thread(new ParameterizedThreadStart(B));
13 t.Start("B");
14
15 Console.Read();
16 }
17
18 private static void B(object obj)
19 {
20 Console.WriteLine("Method {0}!",obj.ToString ());
21
22 }
23 }
24 }
结果显示Method B!
三、带多个参数的
由于Thread默认只提供了这两种构造函数,如果需要传递多个参数,我们可以自己将参数作为类的属性。定义类的对象时候实例化这个属性,然后进行操作。
01 using System;
02 using System.Collections.Generic;
03 using System.Text;
04 using System.Threading;
05
06 namespace AAAAAA
07 {
08 class AAA
09 {
10 public static void Main()
11 {
12 My m = new My();
13 m.x = 2;
14 m.y = 3;
15
16 Thread t = new Thread(new ThreadStart(m.C));
17 t.Start();
18
19 Console.Read();
20 }
21 }
22
23 class My
24 {
25 public int x, y;
26
27 public void C()
28 {
29 Console.WriteLine("x={0},y={1}", this.x, this.y);
30 }
31 }
32 }
结果显示x=2,y=3
四、利用结构体给参数传值。
定义公用的public struct,里面可以定义自己需要的参数,然后在需要添加线程的时候,可以定义结构体的实例。
01 //结构体
02 struct RowCol
03 {
04 public int row;
05 public int col;
06 };
07
08 //定义方法
09 public void Output(Object rc)
10 {
11 RowCol rowCol = (RowCol)rc;
12 for (int i = 0; i < rowCol.row; i++)
13 {
14 for (int j = 0; j < rowCol.col; j++)
15 Console.Write("{0} ", _char);
16 Console.Write("\n");
17 }
18 }
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2