Refresh UI with a Timer in WPF (with BackgroundWorker?)

esylvestre picture esylvestre · Oct 20, 2010 · Viewed 28k times · Source

We have an application in WPF that shows data via ObservableCollection. After 5 minutes, I want to refresh the data.

I thought I could use the System.Timers.Timer object for its Elapsed event and then call a BackgroundWorker to call the method that starts the job. The method is on a ViewModel class.

But it seems that there's a problem the threads.

So I tried with the Dispatcher, but same thing again.

Here's my (simplified and not optimized) code :

/// <summary>
/// Initializes a new instance of the <see cref="ApplicationController"/> class.
/// </summary>
public ApplicationController()
{
    CreateDefaultTabs();

    Timer timer = new Timer(20000); //20 secs for testing purpose.
    timer.AutoReset = true;
    timer.Enabled = true;
    timer.Elapsed += new ElapsedEventHandler(OnTimeBeforeRefreshElapsed);
    timer.Start();
}

private void OnTimeBeforeRefreshElapsed(object sender, ElapsedEventArgs e)
{
    Dispatcher.CurrentDispatcher.Invoke(new Action(() => { RefreshData(); }));
    Dispatcher.CurrentDispatcher.Invoke(new Action(() => { UpdateLayout(); }));
}

private void RefreshData()
{
    foreach (object tab in _tabItems)
    {
        if (tab is TitleDetailsView)
        {
            TitleDetailsViewModel vm = ((TitleDetailsView)tab).DataContext as TitleDetailsViewModel;
            vm.Refresh();
        }
    }
}

private void UpdateLayout()
{
    foreach (object tab in _tabItems)
    {
        if (tab is TitleDetailsView)
        {
            TitleDetailsViewModel vm = ((TitleDetailsView)tab).DataContext as TitleDetailsViewModel;
            vm.HandleGetTitleBySymbolResponse();
        }
    }
}

Any suggestions on how I should proceed?

Answer

Jon Skeet picture Jon Skeet · Oct 20, 2010

Why not use a DispatcherTimer? That will "tick" in the dispatcher thread already.

Beyond that, it's hard to say what's wrong just from your description of "there's a problem with the threads".