locked
supply the worker with the operation parameters RRS feed

  • Question

  • I have a method that has 3 parameters..

    this method is time consuming

    I want to use the background worker to deal with this method..

    but how to supply the worker with that parameters??

    Wednesday, September 7, 2011 2:57 PM

Answers

  • so for "if (!CompressFolder((string)e.Argument[0], (string)e.Argument[1], (string)e.Argument[2]))"

    it thinks of the argument as an object. You need to first cast it to a string array, which is what the underlying object actually is, and then you can use the array accessor method to pull out the strings.

    • Marked as answer by Jackie-Sun Thursday, September 29, 2011 6:44 AM
    Wednesday, September 7, 2011 7:15 PM
  • Hi Dr.X,

    IF you want to pass the paras to the threads, you can try like this:

    You must use constructor accepting ParameterizedThreadStart delegate:

    private void Whatever()
    {
    Thread thread = new Thread(new
    ParameterizedThreadStart(this.ThreadProc));

    int p1 = 10;
    string p2 = "parameter2";
    object[] parameters = new object[] { p1, p2 };

    thread.Start(parameters);
    }

    private static void ThreadProc(object parameters)
    {
    object[] parameterArray = (object[]) parameters;

    int p1 = (int)parameters[0];
    string p2 = (string)parameters[1];
    }

    I hope this cna help you.

     

    have a nice day, 

     


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Thursday, September 29, 2011 6:43 AM
    Tuesday, September 13, 2011 8:39 AM

All replies

  • One thing you can do is supply the Thread with a list of arguments.  For example:

    object args = new object[3] { par1, par2, par3 };
    
    Thread mythread = new Thread(new ParameterizedThreadStart(MyMethod));
    
    mythread.Start(args);
    
    private void MyMethod(object args)
    {
        ...
    }
    
    

     


    Tom Overton
    Wednesday, September 7, 2011 3:12 PM
  • You start it like this:

    int value = ...;
    bgw1.RunWorkerAsync(value);
    

    and then:

    private void worker_DoWork(object sender, DoWorkEventArgs e) 
    {
           int value = (int) e.Argument;
    }
    



    Mitja
    Wednesday, September 7, 2011 3:14 PM
  • Or passing multiple arguments to bgw:

     

    int value = 1;
    string text = "a";
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += (obj, e) => WorkerDoWork(value, text);
    worker.RunWorkerAsync();
    //And on the handler method:
    
    private void WorkerDoWork(int value, string text) 
    {
        ...
    }
    //This way you don't have to start your method with when passing multiple parameters:
    
    private void WorkerDoWork(int value, string text) 
    {
        int value = (int) e.Argument[0];
        string text = (string) e.Argument[1];
        //rest of code...
    }
    





    Mitja
    Wednesday, September 7, 2011 3:17 PM
  • this is the DoWork code:

     private void Worker_CompressProductFolder_DoWork(object sender, DoWorkEventArgs e)
            {
    
                if (!CompressFolder((string)e.Argument[0], (string)e.Argument[1], (string)e.Argument[2]))
                {
                    MyMessage.MessageID = 28;
                    MyMessage.ShowMessage(true);
                    return;
                }
            }

    and this is the rest of code:

    object[] Args = new object[3];
                        Args[0] = "1";
                        Args[1] = "2";
                        Args[2] ="s";
                        Worker_CompressProductFolder.RunWorkerAsync(Args);
    


    I got this error :

    Cannot apply indexing with [] to an expression of type 'object'   

    thanks for the help..

    Wednesday, September 7, 2011 4:01 PM
  • string[] Args = new string[3];
                        Args[0] = "1";
                        Args[1] = "2";
                        Args[2] ="s";
                        Worker_CompressProductFolder.RunWorkerAsync(Args);
    Wednesday, September 7, 2011 4:05 PM
  • still the same error..!
    Wednesday, September 7, 2011 4:11 PM
  • So pass an object, not an object[].

    Example:

                string[] array = new string[2];
                array[0] = "item 1";
                array[1] = "item 2";
                object myArgs = array;
    



    Mitja
    Wednesday, September 7, 2011 4:15 PM
  • Where, specifically, are you getting the error?
    Wednesday, September 7, 2011 4:16 PM
  • And to convert an object back to a string[]:

               //convert back to stirng[]:
                string[] array2 = (string[])myArgs;
    



    Mitja
    Wednesday, September 7, 2011 4:18 PM
  • So pass an object, not an object[].

    Example:

     

                string[] array = new string[2];
                array[0] = "item 1";
                array[1] = "item 2";
                object myArgs = array;
    


     


    Mitja
    The compiler is smart enough to do that on its own, which leads me to believe the error isn't on that line.
    Wednesday, September 7, 2011 4:33 PM
  • the error happens 3 times in this line of code:

     

    if (!CompressFolder((string)e.Argument[0], (string)e.Argument[1], (string)e.Argument[2]))
    

     

    in the implementation of  this method:

     

     

    private void Worker_CompressProductFolder_DoWork(object sender, DoWorkEventArgs e)
    {

    }


     



    • Edited by Dr.X1987 Wednesday, September 7, 2011 6:21 PM
    Wednesday, September 7, 2011 6:20 PM
  • so for "if (!CompressFolder((string)e.Argument[0], (string)e.Argument[1], (string)e.Argument[2]))"

    it thinks of the argument as an object. You need to first cast it to a string array, which is what the underlying object actually is, and then you can use the array accessor method to pull out the strings.

    • Marked as answer by Jackie-Sun Thursday, September 29, 2011 6:44 AM
    Wednesday, September 7, 2011 7:15 PM
  • Hi Dr.X,

    IF you want to pass the paras to the threads, you can try like this:

    You must use constructor accepting ParameterizedThreadStart delegate:

    private void Whatever()
    {
    Thread thread = new Thread(new
    ParameterizedThreadStart(this.ThreadProc));

    int p1 = 10;
    string p2 = "parameter2";
    object[] parameters = new object[] { p1, p2 };

    thread.Start(parameters);
    }

    private static void ThreadProc(object parameters)
    {
    object[] parameterArray = (object[]) parameters;

    int p1 = (int)parameters[0];
    string p2 = (string)parameters[1];
    }

    I hope this cna help you.

     

    have a nice day, 

     


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jackie-Sun Thursday, September 29, 2011 6:43 AM
    Tuesday, September 13, 2011 8:39 AM