In my C# console application, I start a new task and call a function that requires me to be able to access variables that were defined in the main thread, for example:
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;
}
}
The problem is that the above variable value ExampleVarForQuestion is not able to be reached in ExampleVarForQuestion(). I'm quite new to C#, so if this question wasn't worded very good sorry about that.
For it to be reached it must be public as your method is defined in another class.
Note that if the member is going to be written by another thread you should add some form of synchronization to guarantee thread safety.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With