Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately, this can't be solved with a <code>TaskScheduler</code>, because they always work at the <code>Task</code> level, and an <code>async</code> method almost always contains multiple <code>Task</code>s.</p> <p>You should use a <a href="http://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim.aspx" rel="nofollow"><code>SemaphoreSlim</code></a> in conjunction with a prioritizing scheduler. Alternatively, you could use <a href="http://blogs.msdn.com/b/pfxteam/archive/2012/02/12/10266988.aspx" rel="nofollow"><code>AsyncLock</code></a> (which is also included in my <a href="http://nitoasyncex.codeplex.com/" rel="nofollow">AsyncEx library</a>).</p> <pre><code>class Program { private static QueuedTaskScheduler queueScheduler = new QueuedTaskScheduler(targetScheduler: TaskScheduler.Default, maxConcurrencyLevel: 1); private static TaskScheduler ts_priority1; private static TaskScheduler ts_priority2; private static SemaphoreSlim semaphore = new SemaphoreSlim(1); static void Main(string[] args) { ts_priority1 = queueScheduler.ActivateNewQueue(1); ts_priority2 = queueScheduler.ActivateNewQueue(2); QueueValue(1, ts_priority2); QueueValue(2, ts_priority2); QueueValue(3, ts_priority2); QueueValue(4, ts_priority1); QueueValue(5, ts_priority1); QueueValue(6, ts_priority1); Console.ReadLine(); } private static Task QueueTask(Func&lt;Task&gt; f, TaskScheduler ts) { return Task.Factory.StartNew(f, CancellationToken.None, TaskCreationOptions.HideScheduler | TaskCreationOptions.DenyChildAttach, ts).Unwrap(); } private static Task QueueValue(int i, TaskScheduler ts) { return QueueTask(async () =&gt; { await semaphore.WaitAsync(); try { Console.WriteLine("Start {0}", i); await Task.Delay(1000); Console.WriteLine("End {0}", i); } finally { semaphore.Release(); } }, ts); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload