I've started a thread in c#, but I want to be able to abort it and don't really know how to.
private void createThread(){
Thread threadDraw = new Thread(new ThreadStart(myMethod /*Starts a class which loops for a long time*/ ));
threadDraw.Start();
if(myBool == false) {theadDraw.Abort();}
}
myMethod
is starting a class that runs on a loop. The thing is, I want to be able to stop the loop with a keypress (in my case space). Currently, I've tried with
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
/* Checks space press, if space press: make myBool = false */
}
It changes myBool into false and the method itself works.
The problem here is, createThread()
doesn't constantly check if myBool
is false, and thereby doesn't stop.
I've also tried
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
/*checks for spacepress, then {threadDraw.Abort()} */
}
but it doesn't work either, I have tried replacing private with public on createThread
but still, doesn't work.
Additionally, I've been thinking about creating a KeyDown()
-method inside of the class and have a break; in the loop but either I'm doing something wrong (likely) or it's not possible.