I apologize for anything noobish I am about to write, but I am fairly new to multithreading in C#. I have a Windows Form from which I create a new thread:
private void btn_start_Click(object sender, EventArgs e)
{
IMU IMUobject = new IMU();
Thread IMUThread = new Thread(IMUobject.Main);
IMUThread.Start();
}
The function that run in this thread is defined in another class (IMU). The problem is that I'm not able to stop IMUthread in another Windows Form method. In particular I want to stop the thread when I press a button on the form, but this doesn't work as the program tells me that I have a null reference:
private void btn_saveoffset_Click(object sender, EventArgs e)
{
IMUThread.Abort();
}
The null reference is IMUThread: this is strange because I have declared it as a private class in the Windows Form
private Thread IMUThread
{
get;
set;
}
Because of this I cannot call the Abort() method (yes, I know that I shouldn't use Abort, but at the moment I just need this to work).
Do you have any suggestions on how I can correctly stop IMUThread from that event function? And why do I have a null reference on an object that is declared at the beginning of the code?