Reputation: 343
Right now in my UserControl, I have a button click event that starts a thread. I've since moved on from using Abort(), and trying to convert my thread to a background process so that they shut down when I close the parent form. My code is:
public Thread t;
private void btnInitiate_Click(object sender, EventArgs e)
{
UDPListener myListiner = new UDPListener(this);
t.IsBackground = true;
t = new Thread(() => myListiner.SpreadValue(myCurrentPort, firstTicker, secondTicker, myBeta));
t.Start();
}
But when I run the application, I get an error from t.IsBackground=true
where it says "Object reference not set to an instance of an object". I'm wondering where I am going wrong in this case.
Upvotes: 1
Views: 106
Reputation: 2820
You just need to change row order in your code:
...
t = new Thread(() => myListiner.SpreadValue(myCurrentPort, firstTicker, secondTicker, myBeta));
t.IsBackground = true;
...
Because you need to instantiate your thread and only then use it.
Upvotes: 1