This question has already been answered:

There is a class for inverting mouse movement. When calling the Stop method, the program continues to run. When you use Abort prog closes. How to stop the loop in the MouseLoop method?

class Inverter { private Point currentPosition; private bool exit; public void Start() { currentPosition = Cursor.Position; exit = false; (new Thread(new ThreadStart(MouseLoop))).Start(); } public void Stop() {exit = true;} private void MouseLoop() { Thread.CurrentThread.IsBackground = true; Thread.CurrentThread.Priority = ThreadPriority.Highest; while (!exit) { Point newPosition = Cursor.Position; int right = currentPosition.X - (newPosition.X - currentPosition.X); int maxWidth = SystemInformation.VirtualScreen.Width; if (right >= maxWidth) { right -= 3; } else if (right <= 0) { right += 1; } int bottom = currentPosition.Y - (newPosition.Y - currentPosition.Y); int maxHeight = SystemInformation.VirtualScreen.Height; if (bottom >= maxHeight) { bottom -= 3; } else if (bottom <= 0) { bottom += 1; } Cursor.Position = new Point(right, bottom); currentPosition = Cursor.Position; Thread.Sleep(1); }}}} 

Reported as a duplicate by members of tym32167 , MSDN.WhiteKnight , Pavel Mayorov , Community Spirit Aug 6 '18 at 2:43 pm .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • And what do you apply abort if your thread does not have a variable? Can you show the code with the abort method? - Vitaliy Shebanits
  • There is a suspicion that you are jerking the Abort of the main thread. Which leads to the interruption of your program. Another question, what is your processor. If not Intel, IMNIP, you need to declare exit as volatile so that all threads are given the current value. You can also rewrite the code to CancellationTokenSource and pass it as a parameter to the Start stream. - Alex Kuznetsov
  • tell me how to stop the MouseLoop function so that the inversion stops - Natalia Novichenko
  • The answer to your question can be found in the question Stop the flow of work at the press of a button - Alex Kuznetsov
  • Where in your Abort code? - VladD 4:04 pm

1 answer 1

I think it's better to rewrite it like this. I do not know how true this is, but I did so.

 class Inverter { private Point currentPosition; private bool exit; private Thread MouseLoopThread; public void Start() { currentPosition = Cursor.Position; exit = false; if (MouseLoopThread == null || MouseLoopThread.ThreadState == ThreadState.Stopped) { MouseLoopThread = new Thread(new ThreadStart(MouseLoop)); MouseLoopThread.Priority = ThreadPriority.Highest; } if (MouseLoopThread.ThreadState == ThreadState.Unstarted) MouseLoopThread.Start(); } public void Stop() { exit = true; MouseLoopThread.Join(); } private void MouseLoop() { while (!exit) { Point newPosition = Cursor.Position; int right = currentPosition.X - (newPosition.X - currentPosition.X); int maxWidth = SystemInformation.VirtualScreen.Width; if (right >= maxWidth) { right -= 3; } else if (right <= 0) { right += 1; } int bottom = currentPosition.Y - (newPosition.Y - currentPosition.Y); int maxHeight = SystemInformation.VirtualScreen.Height; if (bottom >= maxHeight) { bottom -= 3; } else if (bottom <= 0) { bottom += 1; } Cursor.Position = new Point(right, bottom); currentPosition = Cursor.Position; Thread.Sleep(1); } } } 
  • Why do you think. What is your code better? Please indicate its benefits. - tym32167
  • Well, because I know exactly what thread I am managing, and also because I do not create a bunch of threads when I call Start (). In this context, I do not consider it necessary. If there are better options, I will listen to them with joy, because learning is never too late. - AceMan 1:53 pm
  • I do not blame you, in my question the general duplicate, which I indicated in the comments. What I wrote to you - I did it only to improve the quality of your answer - once the code was given, indicate there (in the answer) at least why it would work better so that the readers of your answer could immediately see your approach to solving the problem without worrying about it. source codes. - tym32167
  • I understand this, I just recently started answering questions and such overlays come out :) - AceMan
  • no problem, thanks for contributing to the community :) - tym32167