前言
线程 被定义为程序的执行路径,每个线程执行特定的工作。当c#程序开始时,主线程自动创建。
线程生命周期
- 未启动状态
- 就绪状态
- 不可运行状态
- 死亡状态
创建无参thread
void acceptthread(){ //todo } thread threadaccept = new thread(new threadstart(acceptthread)); threadaccept.start();
创建有参thread
给线程传递参数有两种方式,一种方式是使用带parameterizedthreadstart
委托参数的thread构造函数,另一种方式是创建一个自定义类
,把线程的方法定义为实例的方法,这样就可以初始化实例的数据,之后启动线程。
方式一:使用parameterizedthreadstart委托
如果使用了parameterizedthreadstart
委托,线程的入口必须有一个object类型的参数,且返回类型为void。且看下面的例子:
using system; using system.threading; namespace threadwithparameters { class program { static void main(string[] args) { string hello = "hello world"; //这里也可简写成thread thread = new thread(threadmainwithparameters); //但是为了让大家知道这里用的是parameterizedthreadstart委托,就没有简写了 thread thread = new thread(new parameterizedthreadstart(threadmainwithparameters)); thread.start(hello); console.read(); } static void threadmainwithparameters(object obj) { string str = obj as string; if(!string.isnullorempty(str)) console.writeline("running in a thread,received: {0}", str); } } }
这里稍微有点麻烦的就是threadmainwithparameters方法里的参数必须是object类型的,我们需要进行类型转换。为什么参数必须是object类型呢,各位看看parameterizedthreadstart委托的声明就知道了。
public delegate void parameterizedthreadstart(object obj); //parameterizedthreadstart委托的声明
方式二:创建自定义类
定义一个类,在其中定义需要的字段,将线程的主方法定义为类的一个实例方法。
using system; using system.threading; namespace threadwithparameters { public class mythread { private string data; public mythread(string data) { this.data = data; } public void threadmain() { console.writeline("running in a thread,data: {0}", data); } } class program { static void main(string[] args) { mythread mythread = new mythread("hello world"); thread thread = new thread(mythread.threadmain); thread.start(); console.read(); } } }
这种方法的缺点在于遇到一个耗时的方法,就新建一个类。
那有什么更好办法既不用强制类型转换,也不用新建一个类呢?
使用匿名方法
方式三:使用匿名方法
using system; using system.threading; namespace threadwithparameters { class program { static void main(string[] args) { string hello = "hello world"; //如果写成thread thread = new thread(threadmainwithparameters(hello));这种形式,编译时就会报错 thread thread = new thread(() => threadmainwithparameters(hello)); thread.start(); console.read(); } static void threadmainwithparameters(string str) { console.writeline("running in a thread,received: {0}", str); } } }
这样既不用类型强制转换也不用新建类就运行成功了。
但是为什么这种方式能行呢,用ildasm反编译后发现,上述说列出来的第三种方式其实和第二种方式是一样的,只不过自定义类编译器帮我们做了。
下面的是第三种方式main方法反编译的il代码:
.method private hidebysig static void main(string[] args) cil managed { .entrypoint // 代码大小 51 (0x33) .maxstack 3 .locals init ([0] class [mscorlib]system.threading.thread thread, [1] class threadwithparameters.program/'<>c__displayclass1' 'cs$<>8__locals2') il_0000: newobj instance void threadwithparameters.program/'<>c__displayclass1'::.ctor() il_0005: stloc.1 il_0006: nop il_0007: ldloc.1 il_0008: ldstr "hello world" il_000d: stfld string threadwithparameters.program/'<>c__displayclass1'::hello il_0012: ldloc.1 il_0013: ldftn instance void threadwithparameters.program/'<>c__displayclass1'::'<main>b__0'() il_0019: newobj instance void [mscorlib]system.threading.threadstart::.ctor(object, native int) il_001e: newobj instance void [mscorlib]system.threading.thread::.ctor(class [mscorlib]system.threading.threadstart) il_0023: stloc.0 il_0024: ldloc.0 il_0025: callvirt instance void [mscorlib]system.threading.thread::start() il_002a: nop il_002b: call int32 [mscorlib]system.console::read() il_0030: pop il_0031: nop il_0032: ret } // end of method program::main
在看看第二种方式的il代码:
.method private hidebysig static void main(string[] args) cil managed { .entrypoint // 代码大小 44 (0x2c) .maxstack 3 .locals init ([0] class threadwithparameters.mythread mythread, [1] class [mscorlib]system.threading.thread thread) il_0000: nop il_0001: ldstr "hello world" il_0006: newobj instance void threadwithparameters.mythread::.ctor(string) il_000b: stloc.0 il_000c: ldloc.0 il_000d: ldftn instance void threadwithparameters.mythread::threadmain() il_0013: newobj instance void [mscorlib]system.threading.threadstart::.ctor(object, native int) il_0018: newobj instance void [mscorlib]system.threading.thread::.ctor(class [mscorlib]system.threading.threadstart) il_001d: stloc.1 il_001e: ldloc.1 il_001f: callvirt instance void [mscorlib]system.threading.thread::start() il_0024: nop il_0025: call int32 [mscorlib]system.console::read() il_002a: pop il_002b: ret } // end of method program::main
比较两端代码,可以发现两者都有一个newobj,这句的作用是初始化一个类的实例,第三种方式由编译器生成了一个类:c__displayclass1
il_0000: newobj instance void threadwithparameters.program/'<>c__displayclass1'::.ctor() il_0006: newobj instance void threadwithparameters.mythread::.ctor(string)
以上就是c#给多线程传参的几种方式小结的详细内容,更多关于c#多线程传参方式的资料请关注代码网其它相关文章!
发表评论