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

  • Home
  • SEARCH
  • 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 161477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:13:12+00:00 2026-05-11T11:13:12+00:00

I want to display time in the Label. The label contents needs to be

  • 0

I want to display time in the Label. The label contents needs to be refreshed automatically as the window is loaded.

I have a simple WPF Window with a Label control in it. As illustrated here

<Window x:Class="shoes.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">     <Grid>         <Label Margin="12" Name="lblSeconds"></Label>         <Button Margin="68,22,135,0" Name="button1" Height="24" VerticalAlignment="Top" Click="button1_Click">Button</Button>     </Grid> </Window> 

I have looked at the code available here : http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh–update-wpf-controls.aspx

And I modified to fit this way :

public partial class Window1 : Window {     private static Action EmptyDelegate = delegate() { };      public Window1()     {         InitializeComponent();              }      private void Window_Loaded(object sender, RoutedEventArgs e)     {         if (IsLoaded)         {             LoopingMethod();         }     }      private void LoopingMethod()     {         while(true)         {             lblSeconds.Content = DateTime.Now.ToLongTimeString();             lblSeconds.Refresh();             Thread.Sleep(10);         }     } } public static class ExtensionMethods {      private static Action EmptyDelegate = delegate() { };       public static void Refresh(this UIElement uiElement)     {         uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);     } } 

I have discovered the code works pretty well when it is triggered through a button_Click event. I have tried to get the code to run through a Window_Loaded event like this but in vain. The window content is never displayed.

What I can do to get the label updated automatically when the window is loaded?

  • 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. 2026-05-11T11:13:12+00:00Added an answer on May 11, 2026 at 11:13 am

    Which is perfectly normal, because the OnLoad handler stays in an infinite loop. That loop is on the UI thread, so the Window never shows.

    First of all: wrap your loop in a Backgroundworker so that it runs on a separate thread.

    I’d also factor out the looping code into a separate object implementing INotifyPropertyChanged which exposes a property with the time (string), have that property raise the PropertyChanged event when it changes (through the loop). You’d still need to do this on a seperate thread of course (eg using BackgroundWorker). Use a Binding to bind your specialized object to the label.

    Another tactic would employ a Timer which does a callback at regular intervals, and you can update the label there.

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

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Lots of good answers here, I wont bother repeating it… May 11, 2026 at 12:46 pm
  • added an answer I've just been through this exact thing, see my previous… May 11, 2026 at 12:46 pm
  • added an answer public static class AssemblyExtensions { public static string InfoToXML(this Assembly… May 11, 2026 at 12:46 pm

Related Questions

I want to display the TIME field from my mysql table on my website,
I want to display from cache for a long time and I want a
At run time I want to dynamically build grid columns (or another display layout)
I want to display tabular type data, but it will not be coming from
I want to display dates in the format: short day of week, short month,
I am trying to update an old VB6 application. I am struggling with changing
I'm using the following piece of Objective-C code to format a NSNumber, and it
I am struggling with the best way to handle a WPF application that has
How can I format data coming from a DataBinder.Eval statement in an ASPX page?
I am building ASP.NET 2.0 websites and currently I some home grown assemblies for

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.