首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于开线程的有关问题

2012-01-23 
关于开线程的问题在类中我定义了一个函数privatevoidtest(stringparaA){..........}现在新建线程来执行tes

关于开线程的问题
在类中我定义了一个函数
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;就行了

热点排行