关于开线程的问题
在类中我定义了一个函数
private void test(string paraA)
{
..........
}
现在新建线程来执行test,以下代码该如何写才正确
string name = "xx ";
ThreadStart newThread = new ThreadStart(this.test); ??
ThreadStart newThread = new ThreadStart(this.test(name)); ??
------解决方案--------------------Thread thread1 = new Thread(new ParameterizedThreadStart(this.test));
thread1.Start(name);
=================2.0
------解决方案--------------------将name定义为全局变量(或类的变量),线程是不能直接用参数的。
string name = "xx ";
private void test()
{
}
name = "xx ";
ThreadStart newThread = new ThreadStart(this.test);
http;//www.psec.net.cn中有非常多现存的例子,例子看多了,自然就会开发软件了。
------解决方案--------------------public TestClass
{
private string _name;
public string Name
{
get{return _name;}
set{_name = value;}
}
public void test()
{
string sName = _name;
..........
}
}
…………………………
TestClass mytest = new TestClass();
mytest.Name = "Ronaldo ";
Thread thread1 = new Thread(new ThreadStart(mytest.test));
thread1.Start();
------解决方案--------------------定义一个类,一个线程初始化一次,再传值就行了
------解决方案--------------------ThreadStart newThread = new ThreadStart(this.test);
------解决方案--------------------哪个说不能用全局变量的?
volatile string name;就行了