Rizowski
Rizowski

Reputation: 3598

Threading a class instantiation, null pointer when using it, C#

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?

Upvotes: 0

Views: 1251

Answers (2)

Marc
Marc

Reputation: 998

If your thread only creates an instance of MyClass and then leaves, you can simply join the thread after creating the manager:

public static void Main(string[] Args)
{
    Manager manager = new Manager();
    manager.myClassThread.Join();
    manager.MyClass.Test();
}

If in your real life code your thread creates an instance of MyClass and then do something else, then you can use for example the AutoResetEvent to wait until MyClass is instantiated:

public class Manager
{
    public MyClass MyClass;
    public AutoResetEvent MyEvent;
    private Thread myClassThread;

    public Manager()
    {
        MyEvent = new AutoResetEvent(false);
        myClassThread = new Thread(() => {
            MyClass = new MyClass();
            MyEvent.Set();
            Thread.Sleep(2000); // long running task
        });
        myClassThread.Start();
    }

    public static void Main(string[] Args)
    {
        Manager manager = new Manager();
        manager.MyEvent.WaitOne();
        manager.MyClass.Test();
    }
}

Upvotes: 0

Paul Keister
Paul Keister

Reputation: 13077

The reason you're getting a null pointer exception is because your worker thread, which constructs the MyClass instance, hasn't executed yet when the call to Manger.Test() happens. This behavior is not guaranteed, it just depends how your threads are scheduled, which is not under your control.

I think it is a good idea for you to continue to learn about threads, and include some study on how to synchronize operations that occur on different threads.

Upvotes: 4

Related Questions