C# 使用GetPosition鼠标移动性能慢

C# 使用GetPosition鼠标移动性能慢,c#,wpf,xaml,transform,C#,Wpf,Xaml,Transform,我有一个画布控件,上面有各种元素,在这个特定的函数中,我允许用户在画布上拖动线条的端点。在MouseMove函数中,我调用e.GetPosition() 根据VS performance analyzer,当应用程序不断移动时,其功能接近总CPU的30%。相当慢。我可以做些什么来提高这种性能 CurrentPoint = e.GetPosition(PointsCanvas); 在windows phone 8上使用MouseMove时,我遇到了同样的问题。似乎在拖动时,事件(包含所需的坐标)

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

根据VS performance analyzer,当应用程序不断移动时,其功能接近总CPU的30%。相当慢。我可以做些什么来提高这种性能

CurrentPoint = e.GetPosition(PointsCanvas);

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

//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 phone 8上使用鼠标移动时,我遇到了同样的问题。似乎在拖动时,事件(包含所需的坐标)会以固定的时间间隔(取决于您在侦听器中的实现,例如每20毫秒一次)引发。所以我所做的就是用我的坐标填充一个,并创建一个线程,通过将第一个元素排队并执行我想要的逻辑来使用该队列。就像这样,逻辑不是串行完成的,因为是另一个线程完成了这项工作。 我不知道我是否足够清楚,请查看下面的代码:

//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)               
        }
    }
简单地说,你在鼠标移动中做的越少,它的速度就越快。 您还可以异步执行逻辑,甚至可以使用它来模拟更多事件。
希望有帮助。

您是否使用了dropshaddow等效果? 我最近遇到过这样的情况,即
e.GetPosition()
也使用了应用程序30%的cpu资源,这没有任何意义,对吗?
我发现在视觉树上有一个控件应用了dropshaddow效果,这就是为什么所有东西都慢了很多…

您是否使用了dropshaddow等效果? 我最近遇到过这样的情况,即
e.GetPosition()
也使用了应用程序30%的cpu资源,这没有任何意义,对吗?
我发现在可视化树上有一个控件应用了dropshaddow效果,这就是为什么所有的东西都慢了很多…

在这个特殊的函数中,我允许用户在画布上拖动一行的端点
-我宁愿使用
拇指
,然后处理
DragDelta
事件。@HighCore您能再解释一下您的意思吗?感谢
在这个特殊的函数中,我允许用户在画布上拖动一条线的端点
-我宁愿使用
拇指
,并处理
DragDelta
事件。@HighCore您能解释一下您的意思吗?谢谢