Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Unsubscribing from the MouseMove event <i>from inside the handler...</i>

Unsubscribing from the MouseMove event <i>from inside the handler...</i>

Scheduled Pinned Locked Moved C#
helptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Edmundisme
    wrote on last edited by
    #1

    What's the explanation for why this doesn't work? What I'm after is a MouseMove handler that does not get called as a result of my programmatic mouse moves. (I know there aren't a lot of good reasons to move the cursor programmatically. This is just an example. I'm after the theory here). The result of this code is that the MouseMove handler is continuously called (as a result of the code that moves the mouse in the MouseMove handler). But why doesn't unsubscribing from the event prior to moving the mouse keep the handler from being called?

    private void OnMouseMove(object sender, MouseEventArgs e)
    {
    // Unsubscribe from the MouseMove event because we don't want to respond to our programmatic mouse move.
    this.MouseMove -= OnMouseMove;
    // Move the mouse one pixel to the right just for kicks...
    Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y);
    // OK, hook it back up.
    this.MouseMove += OnMouseMove;
    }

    My best guess is that this is a threading issue. If that's the case, any ideas on how to fix it? Any ideas? Thanks! Ian

    L 1 Reply Last reply
    0
    • E Edmundisme

      What's the explanation for why this doesn't work? What I'm after is a MouseMove handler that does not get called as a result of my programmatic mouse moves. (I know there aren't a lot of good reasons to move the cursor programmatically. This is just an example. I'm after the theory here). The result of this code is that the MouseMove handler is continuously called (as a result of the code that moves the mouse in the MouseMove handler). But why doesn't unsubscribing from the event prior to moving the mouse keep the handler from being called?

      private void OnMouseMove(object sender, MouseEventArgs e)
      {
      // Unsubscribe from the MouseMove event because we don't want to respond to our programmatic mouse move.
      this.MouseMove -= OnMouseMove;
      // Move the mouse one pixel to the right just for kicks...
      Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y);
      // OK, hook it back up.
      this.MouseMove += OnMouseMove;
      }

      My best guess is that this is a threading issue. If that's the case, any ideas on how to fix it? Any ideas? Thanks! Ian

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      In that event handler, you unsubscribe the event, change the position of mouse pointer, and subscribe again. The event handler will be called again after that. For me, the codes works just fine.

      E 1 Reply Last reply
      0
      • L Lost User

        In that event handler, you unsubscribe the event, change the position of mouse pointer, and subscribe again. The event handler will be called again after that. For me, the codes works just fine.

        E Offline
        E Offline
        Edmundisme
        wrote on last edited by
        #3

        But the handler is called in response to the move, which took place after I unsubscribed, and before I re-subscribed.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups