使用 GetPosition 降低 MouseMove 性能 [英] MouseMove performance slow using GetPosition

查看:32
本文介绍了使用 GetPosition 降低 MouseMove 性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有各种元素的 Canvas 控件,在这个特定的功能中,我允许用户在画布周围拖动一条线的端点.在 MouseMove 函数中,我调用 e.GetPosition().

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().

根据 VS 性能分析器,该函数在不断移动时接近应用程序总 CPU 的 30%.它很慢.我可以做些什么来提高这种性能?

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);

推荐答案

我在 windows phone 8 上使用 MouseMove 时遇到了同样的问题.似乎在拖动时,事件(包含坐标您需要 ) 以固定的时间间隔引发(取决于您在侦听器中的实现方式,例如每 20 毫秒).所以我所做的是填充一个 队列坐标并创建一个线程,通过将第一个元素加入队列并执行我想要的逻辑来消耗该队列.就像这样,逻辑不是连续完成的,因为它是另一个线程在做这项工作.我不知道我是否足够清楚,所以请看下面的代码:

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)               
        }
    }

如果您是 Windows 手机用户,请不要忘记在 Main() 或 MainPage_Loaded(object sender, RoutedEventArgs e) 方法中创建线程.

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();

为了让您在 MouseMove 侦听器中做的事情越简单,速度就会越快.您也可以异步执行逻辑,甚至使用 Bresenham 算法 来模拟更多事件.希望有帮助.

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.

这篇关于使用 GetPosition 降低 MouseMove 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆