C # dynamic task execution in the background

0

I have the following doubt. As I do so that the parameter that I am passing to this function, adapts to the delegate that is assigned to DoWork + = ?? Since the parador is a string, I need the delegate to change based on that string.

public static bool EjecutaTarea(string NombreTareaEjecutar)
        {
            try
            {
                BackgroundWorker tarea = new BackgroundWorker();
                tarea.DoWork += NombreTareaEjecutar;
                tarea.RunWorkerCompleted += Terminado;
                return true;
            }
            catch (Exception ex)
            {
                return false;            }
        }


        static void Tarea1(object obj, DoWorkEventArgs args)
        {

        }
        static void Tarea2(object obj, DoWorkEventArgs args)
        {

        }


        static void Terminado(object obj, RunWorkerCompletedEventArgs args)
        {

        }

greetings

    
asked by Luis Gabriel Fabres 13.03.2017 в 16:22
source

1 answer

2

Maybe through reflection you could get it, but I would not recommend it. It is much easier to make a switch (NombreTareaEjecutar){ case "Tarea1": tarea.DoWork+=Tarea1; break; ....

    
answered by 13.03.2017 в 18:52