0

我在 wpf 中编写了一些代码,如果光标静止几秒钟,我有一个执行点击的鼠标。如果我打开一个我创建的新 wpf 窗口,我想停止点击……但似乎调度员不会停止,即使我几乎尝试了一切......有什么办法吗?

   public DispatcherTimer NewDispacher = new DispatcherTimer();
   public DispatcherTimer NewDispacher2 = new DispatcherTimer();

   public void CreateDispachers() 
   {
       NewDispacher.Tick += new EventHandler(NewDispacher_Tick);
       NewDispacher.Interval = new TimeSpan(0, 0, 0, 0, 10);
       NewDispacher.Start();
       NewDispacher2.Tick += new EventHandler(NewDispacher2_Tick);
       NewDispacher2.Interval = new TimeSpan(0, 0, 0, 4);
       NewDispacher2.Start();
   }

   public void NewDispacher_Tick(object sender, EventArgs e)
    {
        pointcur = GetCursorPosition(); 
    }

   public void NewDispacher2_Tick(object sender, EventArgs e)
    {
        pointdiff = GetCursorPosition();
        if(form1opened==true)
        {   
             NewDispacher.Stop();
             NewDispacher = null;
             NewDispacher2.Stop();
             NewDispacher2 = null;
        }
        else if ((pointdiff.X >= pointcur.X - 5) 
                 && (pointdiff.X <= pointcur.X + 5) 
                 && (pointdiff.Y >= pointcur.Y - 5) 
                 && (pointdiff.Y <= pointdiff.Y + 5))
        {
            DoMouseClick();
            pointcur.X = 0;
        }

    }

当打开新表单时,我将 bool Form1opened=true 设置为 true,但即使它进入 if,调度程序也不会停止......提前谢谢......

4

1 回答 1

1

事实是, Stop() *emphasized text*does*emphasized text* 将 DispatcherTimer 停止在其轨道上。因此,您的代码中的其他地方必须有解释。也许您正在以某种方式重新启动计时器?检查代码何时可以执行实例化/启动计时器,以防它被无意中再次调用。

于 2012-09-09T16:59:50.860 回答