2017-01-15 6 views
1

저는 학교 프로젝트에서 Windows iot C# 개발 SDK를 사용하고 있습니다. 모든 것이 지금까지 훌륭하게 작동하고 있지만 이제는 3 초 및 7 초 후에 트리거해야하는 두 가지 이벤트가 있습니다. 그러나 나는 그것을 작동시킬 수 없다, 나는 지난 2 일 동안 내 머리를 부러 뜨 렸지만, 내가 시도하는 모든 것은 나에게 두통을 훨씬 더 줄 것으로 보인다. 그래서 사람들이 이걸 도와 줄 수 있기를 바랍니다.Dispatchtimer with buttonpresses

기본적으로 프로그램은 버튼 누르기를 기다리고 버튼을 놓은 후에는 타이머를 시작해야하지만 버튼을 누르면 버튼을 다시 놓을 때까지 타이머를 정지해야합니다. 여기 아래로 내 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(); 
      } 
      ); 
     } 
    }