在我的C#控制台应用程序中,我开始一个新任务并调用一个函数,该函数要求我能够访问主线程中定义的变量,例如:
class Example
{
static int ExampleVarForQuestion = 1;
int Main()
{
new Task(WhereProblemOccures.ExampleFunction).Start();
}
}
class WhereProblemOccures
{
static int ExampleFunction()
{
if(Example.ExampleVarForQuestion == 1)
return 1;
else
return 0;
}
}
问题是ExampleVarForQuestion
无法在中达到上述变量值ExampleVarForQuestion()
。我对C#还是很陌生,所以如果未对这个问题进行措辞,对此表示非常抱歉。
要实现它,它必须是公共的,因为您的方法是在另一个类中定义的。
请注意,如果该成员将由另一个线程编写,则应添加某种形式的同步以保证线程安全。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句