我想从线程中调用一个名为UpdateResults()
的非静态方法。这是我的代码:不能使用非静态方法的线程
class Live
{
Thread scheduler = new Thread(UpdateResults);
public Live()
{
scheduler.Start();
}
public void UpdateResults()
{
//do some stuff
}
}
,但我得到这个错误:
A field initializer can not refer to the property, method or non-static field 'Live.UpdateResults()'
我怎样才能解决这个问题?
只是一个问题:在'线程调度;'我得到'场 'LiveScore.scheduler' 分配,但它的价值是永远使用',只是一个警报。 – Dillinger
因为您只在构造函数中使用私有字段。如果你不想在另一个方法中使用这个变量,那么你最好使它在构造函数中是局部的。当你从另一个方法引用变量时,错误将消失 –