Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use a long running Task to process items from a BlockingCollection as suggested by Wilka. Here's an example which pretty much meets your applications requirements. You'll see output something like this:</p> <pre><code>Log from task B Log from task A Log from task B1 Log from task D Log from task C </code></pre> <p>Not that outputs from A, B, C &amp; D appear random because they depend on the start time of the threads but B always appears before B1.</p> <pre><code>public class LogItem { public string Message { get; private set; } public LogItem (string message) { Message = message; } } public void Example() { BlockingCollection&lt;LogItem&gt; _queue = new BlockingCollection&lt;LogItem&gt;(); // Start queue listener... CancellationTokenSource canceller = new CancellationTokenSource(); Task listener = Task.Factory.StartNew(() =&gt; { while (!canceller.Token.IsCancellationRequested) { LogItem item; if (_queue.TryTake(out item)) Console.WriteLine(item.Message); } }, canceller.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); // Add some log messages in parallel... Parallel.Invoke( () =&gt; { _queue.Add(new LogItem("Log from task A")); }, () =&gt; { _queue.Add(new LogItem("Log from task B")); _queue.Add(new LogItem("Log from task B1")); }, () =&gt; { _queue.Add(new LogItem("Log from task C")); }, () =&gt; { _queue.Add(new LogItem("Log from task D")); }); // Pretend to do other things... Thread.Sleep(1000); // Shut down the listener... canceller.Cancel(); listener.Wait(); } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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