0

I found a solution to move the cursor via this URL in my Windows-Form app.
How to move mouse cursor using C#? but as i want to run infinitely but with a break so that when i want to stop it, it should stop here is what i'm trying to achieve.

    private void btnMove_Click(object sender, EventArgs e)
    {
        //int i = 0;
        while (true)
        {
            //i++;
            Cursor = new Cursor(Cursor.Current.Handle);
            Cursor.Position = new Point(Cursor.Position.X - 40, Cursor.Position.Y - 40);
            Thread.Sleep(5000);
            Cursor.Position = new Point(Cursor.Position.X + 40, Cursor.Position.Y + 40);
        }

        //Task t = new Task(() =>
        //{
        //});
        //t.Start();
    }

It works but freezes my code. I just want to run it, and whenever i want to stop it, it should stop not freeze.

  • 1
    `Application.DoEvents()` is an awful solution, which would not even work here at all since it's waiting for 5 seconds in each loop iteration. Look into actual multithreading. – Nyerguds Sep 17 '20 at 11:12
  • 5
    @OlivierRogier every time someone suggests `DoEvents()` to "fix" a problem like this, the world cries a little tear – Marc Gravell Sep 17 '20 at 11:13
  • 3
    @Nyerguds there really is no need for multithreading here; "do something every 5 seconds" is a simple timer – Marc Gravell Sep 17 '20 at 11:13
  • 2
    @OlivierRogier maybe consider: one can love bad things – Marc Gravell Sep 17 '20 at 11:14
  • @OlivierRogier `DoEvents` _does not help_ to keep the UI responsive if it's only executed once every _five seconds_. – Nyerguds Sep 17 '20 at 11:14
  • @MarcGravell I'm fairly sure a timer executing things is technically multithreading. In fact it's in the `System.Threading` namespace... – Nyerguds Sep 17 '20 at 11:17
  • 3
    @Nyerguds There is one in `System.Threading` _and_ [another one](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer?view=netcore-3.1) in `Windows.Forms` – Fildor Sep 17 '20 at 11:18
  • 3
    @Nyerguds there are multiple `Timer` types; the one I'm advocating here is `System.Windows.Forms.Timer`, which is invoked via sync-context back onto the UI context thread; so while there may briefly be a pool thread involved, that implementation detail is not visible to the consumer: from their perspective, only the UI thread is involved – Marc Gravell Sep 17 '20 at 11:18
  • 1
    Huh. Interesting. TIL. – Nyerguds Sep 17 '20 at 11:19
  • Reading again the code I see that this code do nothing but "shaking" the mouse cursor. Like with a virus... or a joke. is it that ? –  Sep 17 '20 at 11:20
  • @OlivierRogier my company folks had decided to put a stupid software to keep track of my movement so thats why i'm using this method to my cursor back and forth –  Sep 17 '20 at 11:21
  • 1
    Well that sounds like an awful company to work for. – DavidG Sep 17 '20 at 11:22
  • Did you post on WorkplaceSE? I think I read something like that just recently. Be aware, that some software may detect this, because it's too "bot-like". You may want to add a little entropy. – Fildor Sep 17 '20 at 11:22
  • @DavidG i know thats why i had to take up this solution for my problem no human can be sitting at his/her desk for 8 hours straight. –  Sep 17 '20 at 11:23
  • @Fildor no i didn't i just posted it here. –  Sep 17 '20 at 11:24
  • 2
    @OlivierRogier There's nothing in the rules to prevent people writing joke software – DavidG Sep 17 '20 at 11:24
  • _"no i didn't i just posted it here"_ - Then you are obviously not alone ... – Fildor Sep 17 '20 at 11:25
  • 1
    I second @DavidG and add: It's not even a joke. It's _"La résistance"_... – Fildor Sep 17 '20 at 11:26
  • @Fildor thank you, don't know how much can do it manually every time so i have to automate it. –  Sep 17 '20 at 11:27
  • @NamanKumar https://www.murgee.com/auto-mouse-mover/ or you can even get hadeware devices that act like mice and just randomly move the mouse occasionally – Marc Gravell Sep 17 '20 at 11:33
  • By the way, software to monitor mouse movement is often quite good at detecting mouse movement as simple as this. Also, if you're not clicking anything, that might flag up too. – DavidG Sep 17 '20 at 11:34
  • @NamanKumar Blocking the mouse like that is as a virus code. If what you tell about a "resistance" is true, you can be sued by your employer for having manufactured and deployed a virus. Don't do that, please. Found another mean. –  Sep 17 '20 at 11:35
  • 1
    @OlivierRogier How is this a virus?! – DavidG Sep 17 '20 at 11:35
  • @MarcGravell thanks for the software to keep in mind it also tracks the software as well .. its name is DeskTime so if they know about a murgee based app running everytime it also might raise a flag so its better to show them i'm running a visual studio app which works as a murgee instead. –  Sep 17 '20 at 11:43
  • 1
    @OlivierRogier That's not a virus, a virus is software that replicates itself. While I'm not advocating the OP does this (my advice would be to get a new job, though that's potentially quite a privileged position to take) this app is not a virus. – DavidG Sep 17 '20 at 11:44
  • 1
    @OlivierRogier And this software is causing no damage. – DavidG Sep 17 '20 at 11:47
  • 1
    @OlivierRogier Did you read the comments? The user isn't losing control or blocking anything. – DavidG Sep 17 '20 at 11:50
  • 1
    @OlivierRogier You could take code from thousands of posts on SO that would be considered malicious if you put them in the wrong place. Like how to send an email, or how can I delete a file. OP has clearly stated what they are trying to do here, and it's not malicious or a virus in any way – DavidG Sep 17 '20 at 11:58

1 Answers1

9

Ultimately, the answer here is: "don't".

Windows forms are based on a message pump. If you have an event-handler from one message (like "click") that loops forever, it will never get around to processing other messages (like "draw"), so: your app is now unresponsive.

Instead of an infinite loop: use a Timer, and move the position in the callback. In this case, a System.Windows.Forms.Timer would be most appropriate.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900