Reputation: 5524
I'm having to work with Threads (First time), and noticed that when I close the form with ALT + F4 The thread continues to run. This is definitely not expected results, as I need this thread to stop running on exit. I currently have:
public void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs E)
{
Continue = false;
}
Boolean Continue = true;
int MouseX = Cursor.Position.X;
int MouseY = Cursor.Position.Y;
Point MousePosition;
public static void LockMouse()
{
Form1 Form = new Form1();
while (Form.Continue)
{
Form.MousePosition.X = 0;
Form.MousePosition.Y = 0;
Cursor.Position = Form.MousePosition;
}
}
and this is thread is being created by:
public void CreteThread()
{
Thread Worker = new Thread(LockMouse);
Worker.Start();
}
I have noticed that the thread stays open when ALT + F4, which is entering a complete mouse lock which is not intended! It seems the form isn't reaching the Closing function?
I'd like to point out, as some users may see this as possible malicious code & Downvote accordingly or request closures for the wrong reason. This application is being created and for use by myself & Will not be distributed
Upvotes: 0
Views: 78
Reputation: 1870
declare your thread as a Background one and it will be destroyed when all other threads ends: IsBackground.
anyhow ... nothing related to your first question ... don't use a while loop to get the coordinates ... use the Eventhandler MouseMove this will be more performant and you get rid of your thread.
Upvotes: 1