✅ 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);
}
}
_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.2 Replies
or you could start a
Stopwatch
and read ElapsedMilliseconds
when you need itUnknown User•3w ago
Message Not Public
Sign In & Join Server To View