Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3842098
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:44:42+00:00 2026-05-19T15:44:42+00:00

I have a WPF app with a textblock that displays the current time. Said

  • 0

I have a WPF app with a textblock that displays the current time. Said textblock is bound to a DependencyProperty on the ViewModel. Naturally I need to keep updating the time, so I used a timer (System.Threading.Timer) like so:

public MainViewModel()
{
    _dateTimer = new Timer(_dateTimer_Tick, null, 0, 60000);
}

void _dateTimer_Tick(object sender)
{
    Time = DateTime.Now.ToString("HH:mm");
    Date = DateTime.Now.ToString("D");
}

The thing is, when the callback is called, the app exits… bummer (output says: “A first chance exception of type ‘System.InvalidOperationException’ occurred in WindowsBase.dll” just when it’s about to write to the Time DP).

If I use a DispatcherTimer everything works fine. I don’t mind using a DispatcherTimer, it’s just that the app is quite big and I wanted to tweak out its performance the best I could. As far as I can see I’m not accessing the UI thread (I’m just updating a property) so a DispatcherTimer wouldn’t be needed.

Am I missing something?

Thanks.

EDIT:
The properties are defined like this:

    public string Time
    {
        get { return (string)GetValue(TimeProperty); }
        set { SetValue(TimeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Time.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TimeProperty =
        DependencyProperty.Register("Time", typeof(string), typeof(MainViewModel), new UIPropertyMetadata(""));

(Date is the same)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T15:44:43+00:00Added an answer on May 19, 2026 at 3:44 pm

    The timer callback is being executed on a thread which is not the UI thread, this causes problems; the reason this is occurring in spite of the fact that you’re ‘only updating a property’ is because such a call creates a chain of calls, namely to notify interested parties of changes to a property, which in this case is the UI, and consequently the scope bubbles up to cause inappropriate cross-thread calls.

    To get over this, you can specify the Dispatcher of your Window in the timer constructor as the argument for the state parameter, then use Dispatcher.Invoke.

    For instance…

    public MainViewModel()
    {
        _dateTimer = new Timer(_dateTimer_Tick, Dispatcher, 0, 60000);
    }
    
    void _dateTimer_Tick(object state)
    {
        ((Dispatcher)state).Invoke(UpdateUI);
    }
    
    void UpdateUI()
    {
        Time = DateTime.Now.ToString("HH:mm");
        Date = DateTime.Now.ToString("D");
    }
    

    EDIT:

    Using a DispatcherTimer, as suggested by Henk, and even considered by yourself, may well be the way to go here, however – I simply wasn’t aware of the type and therefore was in no position to demonstrate in my answer. With regards to the DispatcherTimer and performance, on what grounds is your concern founded?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp
This is an interesting conundrum. We have a WPF app that has a Vista-like
I have a Windows Forms app, that has a single ElementHost containing a WPF
Ok, I have a ResourceDictionary definition that I used for a WPF app below:
I have a WPF app that uses Flash10c.ocx developed on a 32 bit machine.
I have a WPF app that makes use of some multi threading. I am
I have a WPF app which snaps to screen edges (I just set the
I have a WPF App which is grinding to a halt after running out
I'm creating a WPF app and have a system tray icon with a context
I have a mixed UI (Win App, WPF App, and soon an ASP.NET MVC

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.