2017-01-15 38 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(); 
      } 
      ); 
     } 
    }