I want two thread two functions as I stated. Specifically
public static void playerInput()
{
while (true)
{
ConsoleKeyInfo pressedKey = Console.ReadKey();
Doors.input = pressedKey.KeyChar;
Console.Write('b');
}
}
And
public static void openingDoor()
{
while (true)
{
if (Doors.input == 'o')
{
openDoor();
Console.WriteLine($"You're now at door {door}");
input = ' ';
}
}
}
opendoor()
is a function that just increments a value by 1.
I tried doing
Thread pInput = new Thread(playerInput);
Thread doorOpen = new Thread(openingDoor);
pInput.Start();
doorOpen.Start();
pInput.Join();
doorOpen.Join();
At first it seemed working fine, but after pressing o for like 6 times, it crashes my compiler and gives this error:
System.NullReferenceException: Object reference not set to an instance of an object
at System.Timers.Timer.UpdateTimer()
at System.Timers.Timer.set_Enabled(Boolean value)
at CSharpShellCore.IDE.Views.ConsoleView.QueueWriteLine(String text)
at System.Object.CallSite.Target(Closure , CallSite , Object , String )
at CSharpShellCore.Console.WriteLineToView(Object o)
at CSharpShellCore.Console.WriteLine(String n)
at Doors.Doors.openingDoor()
at System.Threading.Thread.StartCallback()
*** **** ***System.NullReferenceException: Object reference not set to an instance of an object
at System.Timers.Timer.UpdateTimer()
at System.Timers.Timer.set_Enabled(Boolean value)
at CSharpShellCore.IDE.Views.ConsoleView.QueueWriteLine(String text)
at System.Object.CallSite.Target(Closure, CallSite, Object, String)
at CSharpShellCore.Console.WriteLineToView(Object o)
at CSharpShellCore.Console.WriteLine(String n)
at Doors.Doors.openingDoor()
at System.Threading.Thread.StartCallback()