Create List of actions already passing the parameters and then run them as parameters passed

Asked

Viewed 48 times

0

I wanted to know if there is the possibility of creating a list of Actions already passing its parameters to execution, because the number that I will pass to the execution of the same can be variable, so I would like to save these aActions already with its parameters for later only run-all at once.

Follow an example:

    public class Program
{
    private static List<Task> ListActions = new List<Task>();

    static void Main(string[] args)
    {
        ExecuteMethod();
        ExecuteListDeActions();
    }

    private static void ExecuteMethod()
    {
        int loop = 10;


        while (loop > 0)
        {
            int valor1 = new Random().Next();
            int valor2 = new Random().Next();
            int valor3 = new Random().Next();

            ListActions.Add(new Task(() => MinhaAction(valor1, valor2, valor3)));

            loop--;
        }
    }

    private async static Task<int> MinhaAction(int valor1, int valor2, int valor3)
    {
        Console.WriteLine("-------------------");
        Console.WriteLine("Numero 1: " + valor1);
        Console.WriteLine("Numero 2: " + valor2);
        Console.WriteLine("Numero 3: " + valor3);
        Console.WriteLine("-------------------");

        return valor1 + valor2 + valor3;
    }

    private async static void ExecuteListDeActions()
    {
        foreach(var a in ListActions)
        {
            a.Start();
        }
        Console.ReadLine();
    }
}
  • It is not duplicated my friend, my question is simple and objective, I would like to create a list of Actions already passing the parameters to them and then just run them understand? And do not pass the values of the parameters once you loop in the list. Understand ?

1 answer

1


I believe that in your case it is better to use Task, since you have an interest in executed all at once.

Reference

public class Program
{
    private List<Task> ListActions = new List<Task>();

    public void Main(string[] args)
    {
        var rand = new Random();
        int valor1 = rand.Next();
        int valor2 = rand.Next();
        int valor3 = rand.Next();
        Task t = Task.Run(() => MinhaAction(valor1, valor2, valor3));
        ListActions.Add(t);
    }

    private int MinhaAction(int valor1, int valor2, int valor3)
    {
        return valor1 + valor2 + valor3;
    }

    private void ExecuteListDeActions()
    {
        Task.WaitAll(ListActions.ToArray());
        foreach (Task t in ListActions)
            Console.WriteLine("Task {0} Status: {1}", t.Id, t.Status);

        Console.WriteLine("T: {0}", ListActions.Count);
    }
}
  • Marcus, that way Invoke asks to pass the parameters to her, I changed the code of my question, a look.

  • I believe that in your case it is better to use Task, since you have an interest in all executed at once. I changed the answer.

  • Okay, I will test this solution friend, I’ll be right back to give you the answer. Thank you.

  • What is the Task.Waitall(Listactions.Toarray()); ?

  • Ensure that all tasks have been completed. https://msdn.microsoft.com/pt-br/library/dd270695(v=vs.110). aspx

  • I could not perform these tasks with the value that is actually passed as parameter, for some reason my return turns out to be all the same numbers.. I’ll edit my code the way I did for you to take a look.

  • -------------------&#Number 1: 1020216779 Number 2: 1020216779 Number 3: 1020216779 _____ ____ Number 1: 1020216779 Number 2: 1020216779 Number 3: 1020216779 Number 2: 1790744707 Number 3: 1790744707

  • Duplicate numbers come out, can you test the code I modified for you to see ? all parameters being random numbers, it is running the routine with the same numbers.

  • The way you are generating Random that is wrong. Try to use what I put in the answer.

  • Perfect, that worked out. One more question, I saw that the methods are executed all together, IE, all at the same time, would have to use a await in this situation? to wait for one task to end and then execute the other. ? This is the first time I’ve used task.

  • I did it and it worked, that’s right ?

  • 1

    foreach(var a in Listactions) { a.Start(); a.Wait(); }

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.