public void Beta()
{
Console.WriteLine("Smile");
}ThreadStart thr_start_func = new ThreadStart(Beta);
Thread fThread = new Thread(thr_start_func);
fThread.Start();
//上面一段程序可以编译通过,可是下面的就不行public void Beta(int a)
{
Console.WriteLine("Smile");
}ThreadStart thr_start_func = new ThreadStart(Beta(2));
Thread fThread = new Thread(thr_start_func);
fThread.Start();
//这里就出现问题啦 ,我只能使用没有参数的函数,但是一旦使用有参数的函数就报错,我应该怎么解决,谢谢大家。
{
Console.WriteLine("Smile");
}ThreadStart thr_start_func = new ThreadStart(Beta);
Thread fThread = new Thread(thr_start_func);
fThread.Start();
//上面一段程序可以编译通过,可是下面的就不行public void Beta(int a)
{
Console.WriteLine("Smile");
}ThreadStart thr_start_func = new ThreadStart(Beta(2));
Thread fThread = new Thread(thr_start_func);
fThread.Start();
//这里就出现问题啦 ,我只能使用没有参数的函数,但是一旦使用有参数的函数就报错,我应该怎么解决,谢谢大家。
new ParameterizedThreadStart(Beta));
// Use the overload of the Start method that has a
// parameter of type Object. You can create an object that
// contains several pieces of data, or you can pass any
// reference type or value type. The following code passes
// the integer value 42.
//
newThread.Start(2);
private void BEAT()
{
Beat(a);
}
带参数的用ParameterizedThreadStart
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;namespace PCTools
{
class Test
{
static void Main(string[] args)
{
Console.WriteLine("========Test Begin========="); //Create
B b = new B("[1]构造函数传递法!"); //Start
new Thread(b.Func).Start(); Console.WriteLine("===========Test End=========");
Console.ReadKey();
}
} class B
{
private string s; public B(string s)
{
this.s = s;
} public void Func()
{
Console.WriteLine(s);
}
}}
using System.Threading;
using System.Runtime.Remoting.Messaging;namespace PCTools
{
class Test
{
static void Main(string[] args)
{
Console.WriteLine("========Test Begin========="); //Create
B b = new B(); //Start
ThreadPool.QueueUserWorkItem(new WaitCallback(b.Func), "[2]线程池传递参数法"); Console.WriteLine("===========Test End=========");
Console.ReadKey();
}
} class B
{
public void Func(string s)
{
Console.WriteLine(s);
} public void Func(object s)
{
this.Func((string)s);
}
}
}
public void Beta(object a)
{
int i = (int)a;
Console.WriteLine("Smile");
}
Thread fThread = new Thread(new ParameterizedThreadStart(Beta));
fThread.Start(2);这样写就可以了
using System.Threading;
using System.Runtime.Remoting.Messaging;namespace PCTools
{
//delegate class Test
{
delegate string MyMethodDelegate(string s); static void Main(string[] args)
{
Console.WriteLine("========Test Begin========="); //Create
B b = new B(); //Start
MyMethodDelegate MyMethod = b.Func;
MyMethod.BeginInvoke("[3]回调法,并且能够处理返回值", CallBack, null); Console.WriteLine("===========Test End=========");
Console.ReadKey();
} static public void CallBack(IAsyncResult result)
{
AsyncResult async = (AsyncResult)result;
MyMethodDelegate DelegateInstance = (MyMethodDelegate)async.AsyncDelegate; Console.WriteLine(DelegateInstance.EndInvoke(result));
}
} class B
{
public string Func(string s)
{
return s;
}
}
}
优点是:方法简洁,
缺点是:要转换类型,最主要是不能处理多参数.[5]回调法,并且能够处理返回值","+多参数版"
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;namespace PCTools
{
//delegate class Test
{
delegate string MyMethodDelegate(string s ,string t); static void Main(string[] args)
{
Console.WriteLine("========Test Begin========="); //Create
B b = new B(); //Start
MyMethodDelegate MyMethod = b.Func;
MyMethod.BeginInvoke("[5]回调法,并且能够处理返回值","+多参数版", CallBack, null); //方法五
Thread fThread = new Thread(b.Func);
fThread.Start("[4]处理单个参数"); Console.WriteLine("===========Test End=========");
Console.ReadKey();
} static public void CallBack(IAsyncResult result)
{
AsyncResult async = (AsyncResult)result;
MyMethodDelegate DelegateInstance = (MyMethodDelegate)async.AsyncDelegate; Console.WriteLine(DelegateInstance.EndInvoke(result));
}
} class B
{
public string Func(string s, string t)
{
return s + t;
} public void Func(string s)
{
Console.WriteLine(s);
} public void Func(object s)
{
this.Func((string)s);
} }
}