title: Asynchronous processing of work items from a priority queue
body: <p>I just got into a mental block when aysncronously handling work items from a queue</p>
  
  <p>Basically multiple calls can come to a loader class to load files(count the number of lines in file- for example's sake). The loader is supposed to queue these work items and process it according to priority. Enqueing can work in parallel, but the actaul loading of file should be happened sequentially.</p>
  
  <p>However my code works syncronously and execute the whole code sequentially.</p>
  
  <pre><code> public class RequestInfo
      {
          public string filename;
          public ManualResetEvent ManaulEvent;
          public int result;
          public int priority;
      }
  
      public class Loader
      {
          readonly ConcurrentPriorityQueue&lt;int, RequestInfo&gt; _loadQueue = new ConcurrentPriorityQueue&lt;int, RequestInfo&gt;();
          public async Task&lt;int&gt; NumOfLinesinFile(string filepath, int priority)
          {
              RequestInfo info = new RequestInfo()
              {
                  filename = filepath,
                  ManaulEvent = new ManualResetEvent(false),
                  priority = priority
              };
              _loadQueue.Enqueue(info.priority, info);
              var task = Task.Factory.StartNew(() =&gt;
              {
                  LoadContent(info);
                  info.ManaulEvent.WaitOne();
              });
              await task;
              return info.result;
          }
  
          async void  LoadContent(RequestInfo info)
          {
              await Task.Run(() =&gt; DoWork());
          }
  
          void DoWork()
          {
              KeyValuePair&lt;int, RequestInfo&gt; quueueContent;
              bool val = _loadQueue.TryDequeue(out quueueContent);
              if (val)
              {
                  quueueContent.Value.result = Load(quueueContent.Value.filename, quueueContent.Value.priority);
                  quueueContent.Value.ManaulEvent.Set();
              }
          }
  
          int Load(string filename, int priority)
          {
              Thread.Sleep(500);
              return priority;
          }
      }
      class Program
      {
          static void Main(string[] args)
          {
              DoFileLoad();
              Console.ReadKey();
          }
          private static async void DoFileLoad()
          {
              Loader loader = new Loader();
  
              Console.WriteLine(await loader.NumOfLinesinFile("C://file1.txt", 1));
              Console.WriteLine(await loader.NumOfLinesinFile("C://file2.txt", 33));
              Console.WriteLine(await loader.NumOfLinesinFile("C://fil3.txt", 0));
              Console.WriteLine(await loader.NumOfLinesinFile("C://fil3.txt", 6));
          }
      }
  </code></pre>
  
  <p>What am I doing wrong?</p>