public class ThreadMethod
{
private int _param = 0;
public ThreadMethod(int param )
{
this._param = param;
}
public void ThreadRun()
{
_param ++ ; //可以在线程中改变其值吗?
}
}
ThreadMethod method = new ThreadMethod( 1 );
Thread t = new Thread( new ThreadStart( method.ThreadRun) );
t.Start();
通过类来向线程传递参数是一种常见的线程参数传递方法。我看网上例子,传递进去的参数都是读,
请问可不可以执行写操作?
{
private int _param = 0;
public ThreadMethod(int param )
{
this._param = param;
}
public void ThreadRun()
{
_param ++ ; //可以在线程中改变其值吗?
}
}
ThreadMethod method = new ThreadMethod( 1 );
Thread t = new Thread( new ThreadStart( method.ThreadRun) );
t.Start();
通过类来向线程传递参数是一种常见的线程参数传递方法。我看网上例子,传递进去的参数都是读,
请问可不可以执行写操作?
{
static void Main(string[] args)
{
Card one = new Card(); one.IFlag = 1;
Thread myThread = new Thread(one.Receive);
myThread.Start();
for (int i = 0; i < 102; i++)
{
one.IFlag++;
Thread.Sleep(50);
}
Console.ReadKey();
}
} public class Card
{
private int iFlag;
public int IFlag
{
get { return iFlag; }
set { iFlag = value; }
} public void Receive()
{
while (this.IFlag < 100)
{
if (this.IFlag % 5 == 0)
{
this.IFlag += 1;
Console.WriteLine(this.IFlag);
} Thread.Sleep(50);
}
Console.WriteLine("线程结束,按回车键退出.");
}
}