raghu_3
raghu_3

Reputation: 1899

System.Timer not available in Metro Apps? What can I use instead of it?

I am trying to poll the server and get JSON at regular intervals but for some reason I cannot see reference System.Timers in a blank app I am writing. This is my code

public MainWindow()
{
    timer.Tick += new EventHandler(CheckJson_Click_1);
    timer.Interval = 30000; //30sec*1000microsec             
    timer.Enabled = true;                       
    timer.Start();
}

private async void CheckJson_Click_1(object sender, RoutedEventArgs e)
{
    var client = new HttpClient();
    client.MaxResponseContentBufferSize = 1024 * 1024;       
    var response = await client.GetAsync(new Uri("URI"));
    var result = await response.Content.ReadAsStringAsync();      
    var component = JsonObject.Parse(result);     
}

Can anybody let me know what I can use instead of timer?

Upvotes: 2

Views: 725

Answers (1)

Michael Petrotta
Michael Petrotta

Reputation: 60912

System.Timers.Timer is usually set to raise events on ThreadPool threads. If you're using that now, you'll want to use the new ThreadPoolTimer class.

If you want to raise events on the UI thread instead, you'll want the new DispatcherTimer, the replacement for System.Windows.Forms.Timer.

Edit: Your comments indicate that you want to use a DispatcherTimer, but you're having trouble with the code. Here's a straight translation of what you've got in the comments:

public void StartTimer(object o, RoutedEventArgs sender)
{ 
    this.timer = new DispatcherTimer();

    // this is an easier way to specify TimeSpan durations
    timer.Interval = TimeSpan.FromMilliseconds(100);

    // you don't need the delegate wrapper
    timer.Tick += checkJson_Tick;

    timer.Start();
}

void checkJson_Tick(object sender, EventArgs e)
{
    // ...
}

Upvotes: 3

Related Questions