0

What would be he correct way to fire a list of task in parallel in a fire and forget manner.

What I've got below makes me believe that .WhenAll is blocking until all is done.

I've got quit a few like these, I need to learn how to loop and store all the called functions and then fire them off where the all run at the same time, it does not matter what function gets called first or last.

What is the correct approach for this?

I wish MS would put in their intellisense a little bit more info to help us out, because I've got more needs for async calls especially a lot of work calls at one time and they're all fire and forget.

Here is what I've got now.

  public async static Task UpdateBayPositionAsync(string cadCNN, string bayPositions)
    {
        List<Task> myTask = new List<Task>();
        string[] bps = bayPositions.Split(',');
        int bID; byte pos;

        for (int i = 0; i < bps.Length; i++)
        {
            bID = int.Parse(bps[i].Split(':')[0].ToString());
            pos = byte.Parse(bps[i].Split(':')[1].ToString());

            myTask.Add(Task.Run(() => { ElevationManagerDL.UpdateBayPosition(cadCNN, bID, pos); }));
        };

        await Task.WhenAll(myTask.ToList());
    }
  • Possible duplicate of [Run two async tasks in parallel and collect results in .NET 4.5](http://stackoverflow.com/questions/12343081/run-two-async-tasks-in-parallel-and-collect-results-in-net-4-5) – Geoff James Jun 23 '16 at 23:01
  • 3
    If you want fire-and-forget, why are you awaiting the tasks??? Edit: Why bother with async? Just shove them all into the threadpool. – leppie Jun 23 '16 at 23:15
  • You're capturing bID and pos from outside the loop then using them asynchronously. There's a real chance that you're not sending the parameters you expect. Declare them inside the loop. – spender Jun 23 '16 at 23:54
  • This is not a duplicate, I am asking how to combine the two, not just parallel. – Filling The Stack is What I DO Jun 24 '16 at 02:55
  • Who is it the trolls around and instead of help, looking to find tid bits on people, this is not a duplicate. The parallel is on this but to do is and run async at the same time is another. Get a life man! – Filling The Stack is What I DO Jun 24 '16 at 02:57
  • @AlumCloud.Com: Are you *sure* you want fire-and-forget? This means: 1) You won't know if an error occurred. 2) You won't know when it completes. 3) (corollary) The code is not unit testable. – Stephen Cleary Jun 24 '16 at 12:38

1 Answers1

0

It looks like you are interested in both asynchronicity and parallelism

I would recommend solving the asynchronicity by a Task (not awaited) and the parallelism with Parallel.ForEach(..)

Parallel.ForEach is a lot more performant than to create one task per position, especially if there are many positions, see Parallel.ForEach vs Task.Factory.StartNew

Something like this

    public async static Task UpdateBayPositionAsync(string cadCnn, string serializedBayPositions)
    {
        string[] bayPositionsAsStrings = serializedBayPositions.Split(',');
        List<BayPosition> bayPositions = bayPositionsAsStrings.Select(bp => new BayPosition(cadCnn, bp)).ToList();

        Task.Factory.StartNew( () => Parallel.ForEach(bayPositions, item => item.Update()));
    }

    public class BayPosition
    {
        public int BId { get; private set; }
        public byte Pos { get; private set; }
        public string CadCnn { get; private set; }

        public BayPosition(string cadCnn, string bayPosition)
        {
            string[] parameters = bayPosition.Split(':');
            BId = Int32.Parse(parameters[0]);
            Pos = Byte.Parse(parameters[1]);
            CadCnn = cadCnn;
        }

        public void Update()
        {
            ElevationManagerDL.UpdateBayPosition(CadCnn, BId, Pos);
        }
    }

And if you only want the parallelism and want to block until all Updates are run then you just replace:

        Task.Factory.StartNew( () => Parallel.ForEach(bayPositions, item => item.Update()));

with

        Parallel.ForEach(bayPositions, item => item.Update());
Community
  • 1
  • 1
Eric Linde
  • 191
  • 6