© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•13mo ago•
4 replies
Tempest

✅ Passing back to the main thread

Hiya

I want to keep track of the amount of milliseconds that have passed in my game. So I've created a thread and told it to run this function.

        private void RunTimeUpdater()
        {
            const int SLEEP_TIME = 50;
            long time = 0;

            while (true)
            {
                DateTime currentTime = DateTime.UtcNow;
                TimeSpan epochTimeSpan = currentTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                time = (long)epochTimeSpan.TotalMilliseconds;
                _model.RecieveNewTime(time);
                Thread.Sleep(SLEEP_TIME);
            }
        }
        private void RunTimeUpdater()
        {
            const int SLEEP_TIME = 50;
            long time = 0;

            while (true)
            {
                DateTime currentTime = DateTime.UtcNow;
                TimeSpan epochTimeSpan = currentTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                time = (long)epochTimeSpan.TotalMilliseconds;
                _model.RecieveNewTime(time);
                Thread.Sleep(SLEEP_TIME);
            }
        }


_model is the root of a composite tree and each branch can handle different things based on how much time since its creation. However doing it this way means that this thread is propagating through my composite and I would much rather have the main thread do it. I have tried using events, task<> and delegates but checking the thread name it always seems to never pass the work off back to the main thread. Is this something that can be done or should I not worry about another thread going through my tree?

Thank you for your help.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Getting back main function
C#CC# / help
3y ago
✅ System.Threading.Channels - Consumer is blocking the main thread where the producer is
C#CC# / help
13mo ago
Very weird params passing error to main Method
C#CC# / help
2y ago
❔ Passing around thread to perform work on?
C#CC# / help
3y ago