110,566
社区成员
发帖
与我相关
我的任务
分享
public static void ThreadProc()
{}
public static void Main()
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
}
关于ThreadProc函数,如果想给他传个参数,是不是只能在public 类里定义public static 数据的方法解决?能不能想想办法按值传递局部变量的方法,主要是想传递的参数更容易销毁。
Thread myThread = new Thread(new ParameterizedThreadStart(ThreadFunc));
myThread.Start(2);
static void ThreadFunc(object param)
{
int value = (int)param;
Console.WriteLine("Thread param: "+ value.ToString());
}
3楼的当然更简洁
int x = 0;
(new Thread(() => { Test(x); })).Start();
void Test(int x)
{
}
public static void ThreadProc()
{
}
public static void ThreadProc(int x, int y)
{
}
Thread t = new Thread(() =>
{
ThreadProc();
int x = 1;
int y = x + 1;
ThreadProc(x, y);
});
t.Start();
你可以随便写任意的语句,甚至可以调用“外层”的变量,例如string a = "张三";
Thread t = new Thread(() =>
{
ThreadProc();
int x = 1;
int y = x + 1;
ThreadProc(x, y);
int z = a.Length;
});
t.Start();
c# 是很优雅的。不是只能简单暴力地调用 ThreadProc 一个函数的。
ParameterizedThreadStart threadStart=new ParameterizedThreadStart(Calculate)
Thread thread=new Thread() ;
thread.Start(0.9);
public void Calculate(object arg)
{
double Diameter=double(arg);
Console.Write("The Area Of Circle with a Diameter of {0} is {1}"Diameter,Diameter*Math.PI);
}