2017-01-15 79 views
1

我正在与Windows IOT的C#开发sdk的学校项目,一切都工作得很好,但现在我有两个事件需要触发3秒和7秒后。但是我无法完成这项工作,过去两天我一直在为这件事烦恼,但我所尝试的一切似乎都让我更加头疼。所以我希望你们能帮助我。按钮按钮之间Dispatchtimer

基本上我的程序等待按钮按下,并且在释放按钮后,我需要启动计时器,但是当按下按钮时,计时器需要被停止,直到再次释放按钮。在这里,你们可以找到我的btn_changed事件。不要把秒表内置的秒表误认为我需要的定时器,秒表具有不同的用途。

问候,吉荣

 private void BtnPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e) 
    { 
     if (e.Edge == GpioPinEdge.FallingEdge) //Execute code when button is pushed down 
     { 
      stopwatch.Restart(); 
      Debug.WriteLine("Button down"); 
     } else // Execute code when button is released 
     { 
      stopwatch.Stop(); 
      if (stopwatch.ElapsedMilliseconds <= 5000) 
      { 
       morsemessage.AddMorse(stopwatch.ElapsedMilliseconds); //Add the new letter 
      } 
      else if (stopwatch.ElapsedMilliseconds > 5000) 
      { 
       morsemessage.AddCharacter(); 
       lcd_writestring(morsemessage.GetDecryptedMessage()); 
      } 
      Debug.WriteLine(morsemessage.currentCharacter); 
      Debug.WriteLine(stopwatch.ElapsedMilliseconds); 
      Debug.WriteLine(morsemessage.Message); 
      Debug.WriteLine(morsemessage.GetDecryptedMessage()); 
      dispatcherTimer.Start(); 
     }   
+0

当您按下按钮并释放按钮时,您能检测到边缘变化的事件吗? –

回答

0

我并不清楚你所遇到的具体问题,但下面这段代码为我工作。你可以试试。

CoreDispatcher dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher; 
    private void InitTimer() 
    { 
     dispatchTimer = new DispatcherTimer(); 
     dispatchTimer.Interval = new TimeSpan(0, 0, 1); 
     dispatchTimer.Tick += (object sender, object e) => 
     { 
      Debug.WriteLine("Timer tick."); 
     }; 
     dispatchTimer.Start(); 
    } 

    private void InitGpio() 
    { 
     button = GpioController.GetDefault().OpenPin(4); 
     button.DebounceTimeout = new TimeSpan(0, 0, 0, 0, 200); 
     button.ValueChanged += BtnPin_ValueChanged; 
    } 

    private async void BtnPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e) 
    { 
     if (e.Edge == GpioPinEdge.FallingEdge) 
     { 
      Debug.WriteLine("Button pressed"); 
      await dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
      { 
       dispatchTimer.Stop(); 
      } 
      ); 
     } 
     else 
     { 
      Debug.WriteLine("Button released"); 
      await dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
      { 
       dispatchTimer.Start(); 
      } 
      ); 
     } 
    }