ThreadStart parameters

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

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

System.Threading.ThreadStart thdStart = new
System.Threading.ThreadStart(MoveMail(new object[] { progress, oItems,
destfolder, objConn });

Where MoveMail has the following parameters :

private void MoveMail(object status)
{
object[] parms = (object[])status;
IProgressCallback callback = (IProgressCallback)parms[0];
Outlook.Items col = (Outlook.Items)parms[1];
Outlook.MAPIFolder destfolder =
(Outlook.MAPIFolder)parms[2];
SqlConnection objConn = (SqlConnection)parms[3];
 
Mike P said:
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
ParameterizedThreadStart 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.
 
I can't find any online examples that actually show the syntax required
for passing parameters to ParameterizedThreadStart. Here is my code
that I have tried that doesn't work :

System.Threading.ThreadStart thdStart = new
System.Threading.ParameterizedThreadStart((MoveMail), new object[] {
progress, oItems, destfolder, objConn });
System.Threading.Thread thd1 = new
System.Threading.Thread(thdStart);
thd1.Start();
 
Mike P said:
I can't find any online examples that actually show the syntax required
for passing parameters to ParameterizedThreadStart. Here is my code
that I have tried that doesn't work :

System.Threading.ThreadStart thdStart = new
System.Threading.ParameterizedThreadStart((MoveMail), new object[] {
progress, oItems, destfolder, objConn });
System.Threading.Thread thd1 = new
System.Threading.Thread(thdStart);
thd1.Start();

The ParameterizedThreadStart constructor only takes the delegate to the
start procedure. The parameter itself is passed on the Start method:

System.Threading.ParameterizedThreadStart thdStart = new
System.Threading.ParameterizedThreadStart(this.MoveMail);
System.Threading.Thread thd1 = new System.Threading.Thread(thdStart);
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).
 
Back
Top