2012-02-01 3 views
1

지연 시간이 n 초 후에 동작/방법을 수행하는 간단한 방법을 찾고 있습니다. 문제는 내가 몇 가지 예를 찾았지만 내 마지막 플랫폼, iOS에서, 그냥n 초 지연 후 동작 수행 WP7 C#

[self performSelector:@selector(methodname) withDelay:3]; 

어떤 조언, 코드 조각이 많이 주시면 감사하겠습니다 때 그들에 대한 지나치게 복잡 보인다.

답변

5

또한 사용할 수있는 대신 DispatcherTimer의 System.Threading.Timer를 사용하여 다음 백그라운드 작업이 타이머를 사용하려면 Scheduler.DispatcherMicrosoft.Phone.Reactive에서 :

Scheduler.Dispatcher.Schedule(MethodName, TimeSpan.FromSeconds(5)); 

private void MethodName() 
{ 
    // This happens 5 seconds later (on the UI thread) 
} 
4
DispatcherTimer DelayedTimer = new DispatcherTimer() 
{ 
    Interval = TimeSpan.FromSeconds(5) 
}; 
DelayedTimer.Tick += (s, e) => 
{ 
    //perform action 
    DelayedTimer.Stop(); 
} 
DelayedTimer.Start(); 
0
DispatcherTimer timer = new DispatcherTimer(); 

    timer.Tick += (s, e) => 
     { 
      // do some very quick work here 

      // update the UI 
      StatusText.Text = DateTime.Now.Second.ToString(); 
     }; 

    timer.Interval = TimeSpan.FromSeconds(1); 
    timer.Start(); 

당신이 여기서하고있는 것은 정말 별도의 스레드에서 아무것도 실행하지 않는, UI 스레드를 중단되어 있습니다. 장기 실행 및 CPU 집중적 인 작업에는 적합하지 않지만 일정한 간격으로 실행해야하는 작업에는 적합합니다. 시계 UI 업데이트가 완벽한 예입니다.

또한 타이머는 시간 간격이 발생할 때 정확하게 실행되지 않을 수도 있지만 시간 간격이 지나기 전에 실행되지 않을 것입니다. 이는 DispatcherTimer 조작이 다른 조작과 마 y 가지로 Dispatcher 대기열에 위!하기 때.입니다. DispatcherTimer 조작이 실행될 때 대기열에있는 다른 작업과 우선 순위에 따라 다 (니다.

For more information use this link

당신은

For more information use this link

1

Windows Phone 8 사용할 수 있습니다

await Task.Delay(milliseconds);