I am still learning a bit about threading. I have a manager class that has some basic business code that calls certain class methods and prepares it for a view. These methods go to manager classes that I am instantiating while creating a new thread. When I try to call the class methods it throws a null pointer even though I know it is working inside its own thread.
What I am assuming is happening is that the current thread that is trying to call the method, cannot access the newly threaded class method. Here is some code to explain:
public class MyClass
{
public void Test()
{
Console.WriteLine("Yay It is working");
}
}
public class Manager
{
public MyClass MyClass;
private Thread myClassThread;
public Manager()
{
myClassThread = new Thread(() => MyClass = new MyClass());
myClassThread.Start();
}
public static void Main(string[] Args)
{
var manager = new Manager().MyClass;
manager.Test();
}
}
I haven't tested to see if this code compiles, so the basic idea behind it is what I am trying to represent. Are my assumptions correct that my current thread can't access the new MyClass test method even though I have access to the variable which was instantiated inside a new thread? How do I solve this? Should I be putting a new thread inside of the Test method instead of in the manager? Is there a standard for multi threading?