Dispatcher Timer

HrckoV

Member
Joined
Oct 19, 2021
Messages
7
Programming Experience
10+
I'm using this code in another class library project.
I won't to call it from mainwindow to change time count in textbox.

C#:
 public class AppTimer
    {
        public void AppTimeCount()
        {
            DispatcherTimer dispatcherTimer = new();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 5, 0);
            dispatcherTimer.Start();

        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
             // what to write in here?
        }
    }
 
Last edited by a moderator:
Inside that timer tick event handler you would update the view model (or the model behind the view model) to which the textbox is bound.

You will also need to add more code to your class so that it will know about the view model or the model that it is supposed to update.

Out to curiosity, why would you update the time count in a textbox? Won't you confuse the user where they will put in a value in the textbox, and the value in the textbox changes all of a sudden? Did you mean a label or text block instead of a textbox?
 
I'm using this textbox to show how much time is user in this app. So, he can't access that textbox.
I changed my code but I still don't know how to update textbox value in mainwindow..

this is a class in class library project:
C#:
public class AppTimer
    {
        DispatcherTimer dispatcherTimer = new();
        public void AppTimeCount()
        {
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);        
            dispatcherTimer.Start();
        }

     
        public string time;
        public int timeSeconds;
        public int timeMinutes;
        public int timeHours;

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            dispatcherTimer.Interval = new TimeSpan(0,0,1);

            timeSeconds += 1;

            if (timeSeconds== 60)
            {
                timeMinutes += 1;
                timeSeconds = 0;
            }

            if (timeMinutes == 59)
            {
                timeHours += 1;
                timeMinutes = 0;
            }            
             
            time = timeHours.ToString() + ":" + timeMinutes.ToString() + ":" + timeSeconds.ToString();       
        
        }

    }

this is mainwindow in main project:

C#:
public partial class MainWindow : Window
    {

         AppTimer appTime = new ();

        public MainWindow()
        {      
            InitializeComponent();      

            this.PreviewKeyDown += new KeyEventHandler(HandleEsc);

            Loaded += login;


            Thread DateTimeThread = new Thread(appTime.AppTimeCount);
            DateTimeThread.IsBackground = true;
            DateTimeThread.Start();

            txtTime.Text = appTime.time ??????
         
        }
        }
 
If you are showing multiple lines of code, please use the multiline code tags. It looks like </>. (Yes, I know that the iconography used in this forum is non-standard. In StackOverflow and other forums, >_ usually suggests multiline code tags.)
 
I'm using this textbox to show how much time is user in this app. So, he can't access that textbox.
You should be using label or text block, not a textbox set to readonly. Please follow the Windows UI guidelines. It's people making up their own UI that gives Windows the bad rap of having inconsistent UI while everyone adores Macs and iOS for their consistency.
 
You should not use the DispatchTimer for time keeping. See the documentation:
Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.
 
This code:
C#:
            timeSeconds += 1;

            if (timeSeconds== 60)
            {
                timeMinutes += 1;
                timeSeconds = 0;
            }

            if (timeMinutes == 59)
            {
                timeHours += 1;
                timeMinutes = 0;
            }            
             
            time = timeHours.ToString() + ":" + timeMinutes.ToString() + ":" + timeSeconds.ToString();

could simply replaced by creating a Stopwatch and the following code:
C#:
time = stopwatch.Elapsed.ToString("h:mm:ss");
 
Back
Top Bottom