none
Code running multiple times at once RRS feed

  • Question

  • Hi All,

    Could you tell me if I am doing this the correct way.

    I have a class that does some data manipulation from and to a database, this class is started from finding a value in a database, so something like so:

            private void Start()
            {
                while (found)
                {
                    var items = dataarm.GetNonProcessedItems();
                    foreach (var item in items)
                    {
                        Processor processor = new Processor();
                        Processor.Run(item);
                    }
                }
            }

    When the processor runs it will call code that may wait for a response from the user or even sleep for a few mins, it also updates the database with values from item. If there is 2 items to process, the 2nd one does not start until the first has finished. What is the best way to achieve having each item run at the same time?

    Regards,

    Loftty

    Friday, May 18, 2018 8:27 AM

Answers

  • Make them async.

    private void void Start ()
    {
       while (found)
       {
          var tasks = new List<Task>();
          var items = dataarm.GetNonProcessedItems();
          foreach (var item in items)
          {
             tasks.Add(Task.Run(() => new Processor().Run(item)));
          };
    
          //Enumerate through and handle any errors that occurred
          foreach (var task in tasks)
          {
             task.Wait();
          };
       }
    }
    

    You could also do this via Parallel.ForEach.

    Note that once you start down the async path you should really go all the way. So your processor should have a RunAsync method that returns a Task that you can wait on rather than using Task.Run. Your Start method should be StartAsync and return a Task so the caller can continue on as well. Basically it bubbles all the way up the call chain.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by loftty Monday, May 21, 2018 8:01 AM
    Friday, May 18, 2018 2:11 PM
    Moderator

All replies

  • Any suggestions please?
    Friday, May 18, 2018 1:49 PM
  • Make them async.

    private void void Start ()
    {
       while (found)
       {
          var tasks = new List<Task>();
          var items = dataarm.GetNonProcessedItems();
          foreach (var item in items)
          {
             tasks.Add(Task.Run(() => new Processor().Run(item)));
          };
    
          //Enumerate through and handle any errors that occurred
          foreach (var task in tasks)
          {
             task.Wait();
          };
       }
    }
    

    You could also do this via Parallel.ForEach.

    Note that once you start down the async path you should really go all the way. So your processor should have a RunAsync method that returns a Task that you can wait on rather than using Task.Run. Your Start method should be StartAsync and return a Task so the caller can continue on as well. Basically it bubbles all the way up the call chain.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by loftty Monday, May 21, 2018 8:01 AM
    Friday, May 18, 2018 2:11 PM
    Moderator
  • Hi Michael,

    That has worked a treat. Thanks you so much.

    Regards,

    Loftty

    Monday, May 21, 2018 8:01 AM