2014-02-25 85 views

回答

10

可以使用DispatcherTimer類

private DispatcherTimer dispatcherTimer; 
    // Constructor 
    public MainPage() 
    { 

     InitializeComponent(); 
     dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 
     dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); 
     dispatcherTimer.Interval = new TimeSpan(0, 0, 1); 
     dispatcherTimer.Start(); 

    } 

    private void dispatcherTimer_Tick(object sender, EventArgs e) 
    { 
    //do whatever you want to do here 
    } 

參考:(http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer(v=vs.110).aspx

1

一個選項可能是使用DispatcherTimer

只需在Tick事件中註冊一個回調。

5

試試這個

public void Start_timer() 
{ 
    DispatcherTimer timer = new DispatcherTimer(); 
    timer.Tick += timer_Tick; 
    timer.Interval = new TimeSpan(00, 0, 10); 
    bool enabled = timer.IsEnabled; 
    timer.Start(); 
} 

void timer_Tick(object sender, object e) 
{ 
    //function to execute 
} 
+0

@克里斯Schiffhauer乙ro我想在後臺進行這種類型的工作。你能否建議如何在後臺使用這個? – Arsal

3

您還可以使用ThreadPoolTimer

TimeSpan period = TimeSpan.FromSeconds(60); 

ThreadPoolTimer PeriodicTimer = ThreadPoolTimer.CreatePeriodicTimer((source) => 
{ 
    // TODO: Work 

    Deployment.Current.Dispatcher.BeginInvoke(() => 
     { 
      // UI update    
     }); 

}, period); 
+0

爲什麼我更喜歡這個簡單的計時器? – eeadev