MouseMove performance slow using GetPosition

孤人 提交于 2021-02-10 18:11:03

问题


I have a Canvas control with various elements on, in this particular function I am allowing a user to drag the end point of a line around the canvas. In the MouseMove function I call e.GetPosition().

The function is, according to the VS performance analyzer, close to 30% of total CPU for the app when constantly moving around. Its pretty slow. What can I do to increase this performance?

CurrentPoint = e.GetPosition(PointsCanvas);

回答1:


I've faced the same problem while using MouseMove on windows phone 8. It seems that while dragging , events (containing the coordinates you need ) are raised at regular time interval ( depending on what you do in the implementation in your listeners, every 20 ms for example). So what I did was to populate a Queue with my coordinates and create a Thread that consume that Queue by enqueue the first element and do the logic I want. Like that the logic is not done serially because it's another thread who does the job. I don't know if I'm enough clear so please take a look to the code below :

//Class used to store e.getPosition(UIElement).X/Y
public class mouseInformation
    {
        public int x { get; set; }
        public int y { get; set; }


        public mouseInformation(int x, int y, String functionName)
        {
            this.x = x;
            this.y = y;              
        }
    }



    private readonly Queue<mouseInformation> queueOfEvent = new Queue<mouseInformation>();

    //MouseMove listener
    private void wpCanvas_MouseDragged(object sender, System.Windows.Input.MouseEventArgs e)
    {
        //Instead of "wpCanvas" put the name of your UIElement (here your canvas name)
        mouseInformation mouseDragged = new mouseInformation((int)e.GetPosition(wpCanvas).X, (int)e.GetPosition(wpCanvas).Y);

        EnqueueMouseEvent(mouseDragged);

    }

    //Allow you to add a MouseInformation object in your Queue
    public void EnqueueMouseEvent(mouseInformation mi)
    {

        lock (queueOfEvent)
        {
            queueOfEvent.Enqueue(mi);
            Monitor.PulseAll(queueOfEvent);
        }
    }

    //Logic that your consumer thread will do
    void Consume()
    {
        while (true)
        {
            mouseInformation MI;

            lock (queueOfEvent)
            {          
                while (queueOfEvent.Count == 0) Monitor.Wait(queueOfEvent);
                MI = queueOfEvent.Dequeue();  
            }

                // DO YOUR LOGIC HERE
                // i.e  DoSomething(MI.x, MI.y)               
        }
    }

And don't forget to create the thread in your Main() or in MainPage_Loaded(object sender, RoutedEventArgs e) method if you are Windows phone user.

 System.Threading.ThreadStart WatchQueue = new System.Threading.ThreadStart(Consume);
 System.Threading.Thread RunWatchQueue = new System.Threading.Thread(WatchQueue);
 RunWatchQueue.Name = "Events thread";
 RunWatchQueue.Start();

To be simple less you do in your MouseMove listener, more speed it will be. You can aswell do the logic asynchronously or even use Bresenham algorithm to simulate more events. Hope it helps.




回答2:


Are you using any effects such as dropshaddow etc? I recently had the situation where e.GetPosition() was also using 30% of the app's cpu resources, which doesn't make any sense right? I turns out that up the visual tree there was a control applying a dropshaddow effect and that was what was slowing everything down so much...



来源:https://stackoverflow.com/questions/16321755/mousemove-performance-slow-using-getposition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!