Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What it costs to use Task.Delay()?

I am thinking on using C# async\await in MMO game server with event-driven logic. Let's assume there are thousands of entities doing some work with known durations. So I would like to invoke Time.Delay() for every of my game objects. (This is an opposit approach to common infinite loop with some Update() call for every game object.)

Does anybody knows how is Task.Delay() implemented? Is it using timers? Is it heavy on system resources?

Is it okay to spawn thousands of simultaneous Task.Delay() invocations?

like image 315
Anton Petrov Avatar asked Jun 29 '13 09:06

Anton Petrov


People also ask

What is Task delay?

The Delay method is typically used to delay the operation of all or part of a task for a specified time interval. Most commonly, the time delay is introduced: At the beginning of the task, as the following example shows.

Does task delay block thread?

Delay(1000) doesn't block the thread, unlike Task. Delay(1000).


1 Answers

Task.Delay is implemented as follows:

public static Task Delay(int millisecondsDelay, CancellationToken cancellationToken)
{
  //error checking
  Task.DelayPromise delayPromise = new Task.DelayPromise(cancellationToken);
  if (cancellationToken.CanBeCanceled)
    delayPromise.Registration = cancellationToken.InternalRegisterWithoutEC((Action<object>) (state => ((Task.DelayPromise) state).Complete()), (object) delayPromise);
  if (millisecondsDelay != -1)
  {
    delayPromise.Timer = new Timer((TimerCallback) (state => ((Task.DelayPromise) state).Complete()), (object) delayPromise, millisecondsDelay, -1);
    delayPromise.Timer.KeepRootedWhileScheduled();
  }
  return (Task) delayPromise;
}

It definitely uses timers. They're used in a class called DelayPromise. Here's the implementation for that:

private sealed class DelayPromise : Task<VoidTaskResult>
{
  internal readonly CancellationToken Token;
  internal CancellationTokenRegistration Registration;
  internal Timer Timer;

  internal DelayPromise(CancellationToken token)
  {
    this.Token = token;
  }

  internal void Complete()
  {
    if (!(this.Token.IsCancellationRequested ? this.TrySetCanceled(this.Token) : this.TrySetResult(new VoidTaskResult())))
      return;
    if (this.Timer != null)
      this.Timer.Dispose();
    this.Registration.Dispose();
  }
}

It does use a timer, but it doesn't seem like a worry to me. The timer just calls back to the complete method, and what that does is check if it's canceled, if so cancel it, else just return a result. It seems fine to me.

like image 105
It'sNotALie. Avatar answered Sep 19 '22 03:09

It'sNotALie.