jeudi 16 février 2017

Correct way to handle BackGroundWorker inheritance with Action and a collection of parameters?

To clean up code that will use BackgroundWorker often and with the same settings, I created the following class as a helper:

     public class MyBackGroundWorker : System.ComponentModel.BackgroundWorker
            {
                private Action _action;
                private Action _stopAction;    
                public List<dynamic> Parameters { get; set; } = new List<dynamic>();

                MyBackGroundWorker(Action method, Action doWhenFinished = null)
                {
                    this._action = method;
                    this._stopAction = doWhenFinished;
                    Init();

                }`

"Init()" contains all of the settings that will be applied each time:

private void Init()
            {
                this.WorkerReportsProgress = true;
                this.WorkerSupportsCancellation = true;
                this.DoWork += new DoWorkEventHandler(StartWork);
                this.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkDone);
            }

And the handlers and public methods are:

            private void StartWork(object sender, DoWorkEventArgs e)
            {
                if (Parameters.Count() > 0)
                {

                    ParameterInfo[] pi = _action.Method.GetParameters();
                    //now i'm stuck!!!!!

                }
                else
                {
                    _action.Method.Invoke(null, this.Parameters);
                    //i know this is wrong, wrote it in to illustrate the goal
                }
            }

            private void WorkDone(object sender, RunWorkerCompletedEventArgs e)
            {
                _stopAction.Method.Invoke(null, null); //>.<
            }
            public void Go()
            {
                RunWorkerAsync();
            }

            public void Stop()
            {
                this.CancelAsync();
            }

Obviously there are some problems here. The end goal is to initialize the object with a method to start running, a method to do at the end (optional), and a parameter collection one adds to before running. A calling class might have a section like this:

using(var cmd = new MyBackGroundWorker(method1, StopLoadingSpinner))
{
  cmd.Parameters.Add("Stringparam");
  cmd.Parameters.Add(1);
  StartLoadingSpinner();
  cmd.Go();
}

I've looked some things up involving delegates to try to sort this out, but I know that's only one piece of the puzzle. From there, my searches online have been fruitless, as I'm not sure in what direction I need to research. Where do I go from here? The idea itself may be fundamentally flawed, and I'm willing to accept that, but what can I do to accomplish my goal of a BackgroundWorker so simply used by the calling method, while using variable types and counts of parameters based on the method I'm trying to run in the background?





Aucun commentaire:

Enregistrer un commentaire