Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Simpliest code: Can't change label "Content" programmatically

Simpliest code: Can't change label "Content" programmatically

Scheduled Pinned Locked Moved WPF
wpfcsharpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pew_new
    wrote on last edited by
    #1

    Hello everyone. Currently I am trying to work with timers in WPF.

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    _timer = new System.Timers.Timer();
    _timer.Interval = 1000;
    _timer.Elapsed += OnTimerElapsed;
    _timer.Start();

        }
    
        int i=0;
        private void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            i++;
    
            label1.Content = i.ToString();
    
        }
    

    I've got an exception at this string

    label1.Content = i.ToString();

    What I'm doing wrong with a label? Thanks in advance! XAML:

    B 1 Reply Last reply
    0
    • P Pew_new

      Hello everyone. Currently I am trying to work with timers in WPF.

      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
      _timer = new System.Timers.Timer();
      _timer.Interval = 1000;
      _timer.Elapsed += OnTimerElapsed;
      _timer.Start();

          }
      
          int i=0;
          private void OnTimerElapsed(object sender, ElapsedEventArgs e)
          {
              i++;
      
              label1.Content = i.ToString();
      
          }
      

      I've got an exception at this string

      label1.Content = i.ToString();

      What I'm doing wrong with a label? Thanks in advance! XAML:

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      Pew_new wrote:

      What I'm doing wrong with a label?

      You access it from a different thread - you must not do that, and that holds true also for other UI technologies like WinForms. With WPF also comes MVVM. Instead of setting the Content property directly, you bind it to some property of your view model. When that property changes, you raise an PropertyChanged event, and that will do the magic of "transporting" the change in the correct thread.

      Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

      P 1 Reply Last reply
      0
      • B Bernhard Hiller

        Pew_new wrote:

        What I'm doing wrong with a label?

        You access it from a different thread - you must not do that, and that holds true also for other UI technologies like WinForms. With WPF also comes MVVM. Instead of setting the Content property directly, you bind it to some property of your view model. When that property changes, you raise an PropertyChanged event, and that will do the magic of "transporting" the change in the correct thread.

        Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

        P Offline
        P Offline
        Pew_new
        wrote on last edited by
        #3

        Please explain where have you seen " different thread"? "different thread", you mean that with a System.Timer it's not possible? P.S. Here https://www.youtube.com/watch?v=QkT8fgoFz3g, a guy is doing that easily. True he's using a Dispatcher Timer. So what's difference?

        Richard DeemingR 1 Reply Last reply
        0
        • P Pew_new

          Please explain where have you seen " different thread"? "different thread", you mean that with a System.Timer it's not possible? P.S. Here https://www.youtube.com/watch?v=QkT8fgoFz3g, a guy is doing that easily. True he's using a Dispatcher Timer. So what's difference?

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          The DispatcherTimer documentation makes things slightly clearer than the System.Timers.Timer documentation:

          DispatcherTimer Class (System.Windows.Threading) | Microsoft Docs[^]:

          If a System.Timers.Timer is used in a WPF application, it is worth noting that the System.Timers.Timer runs on a different thread than the user interface (UI) thread. In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of the user interface (UI) thread using Invoke or BeginInvoke. Reasons for using a DispatcherTimer as opposed to a System.Timers.Timer are that the DispatcherTimer runs on the same thread as the Dispatcher and a DispatcherPriority can be set on the DispatcherTimer.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups