0

I have a WPF application with a DispatcherTimer in UI Thread.

    private int TimeLimit = 500;
    private DispatcherTimer _timer;

    public MainWindow()
    {
        _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(TimeLimit) };
        _timer.Tick += TimerTick;
        _timer.Start();
        InitializeComponent();
    }
    private void TimerTick(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " Timer");
    }

And when I hover a button, that implement Drag logic, my DispatcherTimer are holding.

xaml:

<Button Width="200" Height="50" Content="Test"   PreviewMouseMove="_MouseMove"/>

code:

        private Point startPoint = new Point();
        private void _MouseMove(object sender, MouseEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fff") + " MousePosition");

            // Get the current mouse position
            Point mousePos = e.GetPosition(null);
            Vector diff = startPoint - mousePos;
        if (e.LeftButton == MouseButtonState.Pressed &&
            Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
            Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
        {
            // Get the dragged Item
            Button item = sender as Button;
            if (item != null)
            {
                // Get the data
                String contact = (String)item.Content;

                // Initialize the drag & drop operation
                DataObject dragData = new DataObject("String", contact);
                DragDrop.DoDragDrop(item, dragData, DragDropEffects.Move);
            }
        }
    } 

I must to use working DispatcherTimer. How can I make friends DoDragDrop and the DispatcherTimer?

Dr_klo
  • 469
  • 6
  • 19
  • 1
    The snippets do not demonstrate the problem. There is a subtle bug in the _MouseMove event handler, the if() statement requires parentheses. Easy to tell from the flickering cursor. – Hans Passant Jul 02 '15 at 10:14
  • @HansPassant The bug fixed, thanks. You can say, why flickering cursor hold UI thread? Because early the timer holding even not entering in 'if' branch. – Dr_klo Jul 02 '15 at 10:35

0 Answers0