两种简单的方法:using System; using System.Threading;namespace ConsoleApplication2 { class Class1 { [STAThread] static void Main(string[] args) { new eg1().Start(); new eg2().Start(); Console.ReadLine(); } } class eg1 { public void Start() { new DoJobDelegate(DoJob).BeginInvoke("aaa async",null,null); DoJob("aaa sync"); } private delegate void DoJobDelegate(string msg); private void DoJob(string msg) { Console.WriteLine(msg); } } class eg2 { public void Start() { new DoJobScope(this,"bbb async"); DoJob("bbb sync"); } private class DoJobScope { eg2 p; string msg; public DoJobScope(eg2 p,string msg) { this.p=p; this.msg=msg; new Thread(new ThreadStart(Start)).Start(); } private void Start() { p.DoJob(msg); } } private void DoJob(string msg) { Console.WriteLine(msg); } } }
同意刀兄的方法,例:class a{ public string b = ""; public void c(){ //todo: code here } } class d{ a obj = new a(); obj.b = "ok"; //传参 Thread t = new Thread(new ThreadStart(a.c)); t.Start(); }
<FrameworkSDK>\Samples\Technologies\Threading\下的例子。
using System.Threading;namespace ConsoleApplication2
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
new eg1().Start();
new eg2().Start(); Console.ReadLine();
}
}
class eg1
{
public void Start()
{
new DoJobDelegate(DoJob).BeginInvoke("aaa async",null,null);
DoJob("aaa sync");
}
private delegate void DoJobDelegate(string msg);
private void DoJob(string msg)
{
Console.WriteLine(msg);
}
}
class eg2
{
public void Start()
{
new DoJobScope(this,"bbb async");
DoJob("bbb sync");
}
private class DoJobScope
{
eg2 p;
string msg;
public DoJobScope(eg2 p,string msg)
{
this.p=p;
this.msg=msg;
new Thread(new ThreadStart(Start)).Start();
}
private void Start()
{
p.DoJob(msg);
}
}
private void DoJob(string msg)
{
Console.WriteLine(msg);
}
}
}
public string b = "";
public void c(){
//todo: code here
}
}
class d{
a obj = new a();
obj.b = "ok"; //传参
Thread t = new Thread(new ThreadStart(a.c));
t.Start();
}