hammer
hammer

Reputation: 155

WPF Limit mouse move message frequency

other than rolling my own method with some sort of timer, is there a better more general way to limit how often you get mouse move messages?

I'm doing some quite intensive processing on a mouse move message and would like to limit the frequency to something like 30 updates a second. At the moment it appears I can receive hundreds a second.

Upvotes: 2

Views: 2276

Answers (2)

DerPrzem
DerPrzem

Reputation: 133

Well, this comes late, but I solved a similar problem by checking the elapsed time of a stopwatch, and calling my internal function only every a specific number of ticks. The interval is define more or less empirically, but maybe one can refine this approach to get a more exact results with milliseconds. In my case the solution worked like a charm.

    public static Stopwatch stopwatch = new Stopwatch();

    public static void OnMouseMove(object sender, RoutedEventArgs e)
    {
            if (stopwatch.ElapsedTicks % 10L == 0L)
            {
                // do the heavy work here....
            }
        }
    }

Of course you have to start the stopwatch somewhere...

Upvotes: 2

Michael Sanderson
Michael Sanderson

Reputation: 90

From what I understand the MouseMove event fires every time the mouse moves; that is every time the x,y coordinate of the pointer is changed. Because of this, I don't believe there is a way to limit the messages/sec that are fired by the event as this is simply determined by how often you move the mouse. What I would suggest is if there are only certain times you want the move event to fire, you could add and remove your event handler at these times. You are more than welcome to continue searching for a method to implement your desired functionality, and if you do I would be very interested to see it. Here is the MSDN article on the MouseMove event, which is probably a good place to start looking. Good luck!

Upvotes: 2

Related Questions