Empeld
Empeld plugin documentation.
pluginbase.Objects.Tasks.TaskEx Class Reference

Static Public Member Functions

static async Task SpinWait (Func< bool > until, int pollMillis=10, int timeoutMillis=-1)
 Wait for a predicate to become true via polling More...
 
static TaskAwaiter< bool > GetAwaiter (this IScheduledTask task)
 GetAwaiter on a scheduled task (allows you to await the task) More...
 
static Task< T > QueueAsyncTask< T > (this ITaskScheduler scheduler, Func< T > func)
 Queues an async task on the scheduler to result in an awaitable task Only use if you know what you're doing. Much of empeld is not async-safe More...
 
static Task QueueAsyncTask (this ITaskScheduler scheduler, Action func)
 Queues an async task on the scheduler to be an awaitable task Only use if you know what you're doing. Much of empeld is not async-safe More...
 

Member Function Documentation

◆ GetAwaiter()

static TaskAwaiter<bool> pluginbase.Objects.Tasks.TaskEx.GetAwaiter ( this IScheduledTask  task)
static

GetAwaiter on a scheduled task (allows you to await the task)

Returns
The awaiter.
Parameters
taskTask.
31  {
32  var tcs = new TaskCompletionSource<bool>();
33 
34  if (task.IsDone)
35  {
36  tcs.SetResult(true);
37  }
38  else
39  {
40  task.Finish += (t) => tcs.SetResult(true);
41  }
42 
43  return tcs.Task.GetAwaiter();
44  }

◆ QueueAsyncTask()

static Task pluginbase.Objects.Tasks.TaskEx.QueueAsyncTask ( this ITaskScheduler  scheduler,
Action  func 
)
static

Queues an async task on the scheduler to be an awaitable task Only use if you know what you're doing. Much of empeld is not async-safe

Returns
The async task.
Parameters
schedulerScheduler.
funcFunc.
80  {
81  return QueueAsyncTask<bool>(scheduler, () => {
82  func();
83  return true;
84  });
85  }

◆ QueueAsyncTask< T >()

static Task<T> pluginbase.Objects.Tasks.TaskEx.QueueAsyncTask< T > ( this ITaskScheduler  scheduler,
Func< T >  func 
)
static

Queues an async task on the scheduler to result in an awaitable task Only use if you know what you're doing. Much of empeld is not async-safe

Returns
The async task.
Parameters
schedulerScheduler.
funcFunc.
Template Parameters
TThe 1st type parameter.
55  {
56  var tcs = new TaskCompletionSource<T>();
57 
58  scheduler.QueueAsync(() => {
59  try
60  {
61  tcs.SetResult(func());
62  }
63  catch(Exception e)
64  {
65  tcs.SetException(e);
66  }
67  });
68 
69  return tcs.Task;
70  }

◆ SpinWait()

static async Task pluginbase.Objects.Tasks.TaskEx.SpinWait ( Func< bool >  until,
int  pollMillis = 10,
int  timeoutMillis = -1 
)
static

Wait for a predicate to become true via polling

Returns
The wait.
Parameters
untilUntil.
pollMillisPoll millis.
timeoutMillisTimeout millis.
16  {
17  int totalWait = 0;
18  while(!until() && (timeoutMillis < 0 || totalWait < timeoutMillis))
19  {
20  await Task.Delay(pollMillis);
21  totalWait += pollMillis; // Not exact, but close enough (better than timing)
22  }
23  }

The documentation for this class was generated from the following file: