【问题标题】:MouseMove performance slow using GetPosition使用 GetPosition 的 MouseMove 性能变慢
【发布时间】:2013-05-01 16:02:12
【问题描述】:

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

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

CurrentPoint = e.GetPosition(PointsCanvas);

【问题讨论】:

  • in this particular function I am allowing a user to drag the end point of a line around the canvas - 我宁愿为此使用Thumb 并处理DragDelta 事件。
  • @HighCore 你能解释一下你的意思吗?谢谢

标签: c# wpf xaml transform


【解决方案1】:

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

//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) 方法中创建线程。

 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 algorithm 模拟更多事件。 希望对您有所帮助。

【讨论】:

    【解决方案2】:

    你是否使用了dropshaddow等效果? 我最近遇到e.GetPosition() 也使用了应用程序 30% 的 cpu 资源的情况,这没有任何意义吧? 我发现在视觉树上,有一个控件应用了阴影效果,这就是让一切都变慢的原因......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 2012-08-17
      • 2020-02-18
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多