ThreadStart parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    #1

    ThreadStart parameters

    How do you pass your parameters to the method you have specified for the
    threadstart?

    System.Threadin g.ThreadStart thdStart = new
    System.Threadin g.ThreadStart(M oveMail(new object[] { progress, oItems,
    destfolder, objConn });

    Where MoveMail has the following parameters :

    private void MoveMail(object status)
    {
    object[] parms = (object[])status;
    IProgressCallba ck callback = (IProgressCallb ack)parms[0];
    Outlook.Items col = (Outlook.Items) parms[1];
    Outlook.MAPIFol der destfolder =
    (Outlook.MAPIFo lder)parms[2];
    SqlConnection objConn = (SqlConnection) parms[3];



    *** Sent via Developersdex http://www.developersdex.com ***
  • Alberto Poblacion

    #2
    Re: ThreadStart parameters

    "Mike P" <mike.parr@gmai l.comwrote in message
    news:Ozt0pn5aHH A.4520@TK2MSFTN GP06.phx.gbl...
    How do you pass your parameters to the method you have specified for the
    threadstart?
    If you have version 2.0 of the framework you can use
    ParameterizedTh readStart instead of ThreadStart. Otherwise, the trick is to
    create a class with member variables for the parameters and a start method
    without parameters inside the class. Create an object from that class, set
    the parameters into the member variables, and start the thread from the
    parameter-less start method inside the class. This dumy method can then call
    your real start method passing to it the parameters from the member
    variables.


    Comment

    • Mike P

      #3
      Re: ThreadStart parameters

      I can't find any online examples that actually show the syntax required
      for passing parameters to ParameterizedTh readStart. Here is my code
      that I have tried that doesn't work :

      System.Threadin g.ThreadStart thdStart = new
      System.Threadin g.Parameterized ThreadStart((Mo veMail), new object[] {
      progress, oItems, destfolder, objConn });
      System.Threadin g.Thread thd1 = new
      System.Threadin g.Thread(thdSta rt);
      thd1.Start();


      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: ThreadStart parameters

        On Mar 21, 9:18 am, Mike P <mike.p...@gmai l.comwrote:
        How do you pass your parameters to the method you have specified for the
        threadstart?
        See http://pobox.com/~skeet/csharp/threads/parameters.shtml

        Jon

        Comment

        • Alberto Poblacion

          #5
          Re: ThreadStart parameters

          "Mike P" <mike.parr@gmai l.comwrote in message
          news:%23C166Q6a HHA.4832@TK2MSF TNGP02.phx.gbl. ..
          >I can't find any online examples that actually show the syntax required
          for passing parameters to ParameterizedTh readStart. Here is my code
          that I have tried that doesn't work :
          >
          System.Threadin g.ThreadStart thdStart = new
          System.Threadin g.Parameterized ThreadStart((Mo veMail), new object[] {
          progress, oItems, destfolder, objConn });
          System.Threadin g.Thread thd1 = new
          System.Threadin g.Thread(thdSta rt);
          thd1.Start();
          The ParameterizedTh readStart constructor only takes the delegate to the
          start procedure. The parameter itself is passed on the Start method:

          System.Threadin g.Parameterized ThreadStart thdStart = new
          System.Threadin g.Parameterized ThreadStart(thi s.MoveMail);
          System.Threadin g.Thread thd1 = new System.Threadin g.Thread(thdSta rt);
          thd1.Start(new object[] {progress, oItems, destfolder, objConn });

          Notice that you can only pass a SINGLE parameter of type object. You can
          put anyting inside an object, in this case an array of objects that contains
          four elements. Be sure to adjust your MailMove method accordingly (it should
          take only one object, not four, and then extract those four objects from
          inside the one that you passed).

          Comment

          Working...