Passing parameter to thread

  • Thread starter Thread starter Hardy Wang
  • Start date Start date
H

Hardy Wang

Hi, All
I am wandering in 1.1 version of C#, how can I pass parameters to
thread?
provides a way to pass one parameter. It looks like not possible to pass
more than one.

Anybody has a solution?
 
Hardy Wang said:
Hi, All
I am wandering in 1.1 version of C#, how can I pass parameters to
thread?
provides a way to pass one parameter. It looks like not possible to pass
more than one.

Anybody has a solution?

The solution on the website shows an example with one parameter. The
UrlFetcher class has a "parameter" called url. You could easily add other
parameters/properties to this class, set them appropriately, and then start
the thread which uses them (a function called Fetch).
 
just create an object and pass it to the thread. Here is code i did for a
email blaster
mail = new smtp();
mail.to = txtTo.Text;
mail.from = txtFrom.Text;
mail.name = txtName.Text;
mail.subj = txtSub.Text;
mail.body = txtBody.Text;
mail.ssrv = ssrv;
mail.listBox1 = listBox1;
ThreadPool.QueueUserWorkItem(new WaitCallback(mail.Mail), i);
}*/


my code was using an array of object so ignore that =)

you can use that to control what thread number or index your on etc..

Michael Evanchik
not a MVP
www.michaelevanchik.com
 
i just relized i had that commented out (sorry notepad has no colors) but im
sure this code worked if you can follow


private void mailit()
{

smtp mail = new smtp();
mail.to = txtTo.Text;
mail.from = txtFrom.Text;
mail.name = txtName.Text;
mail.subj = txtSub.Text;
mail.body = txtBody.Text;
mail.label9 = label9;
//mail.pool = listBox2;
mail.srv = ssrv;
mail.inx = ind.ToString();



mail.listBox1 = listBox1;
mail.amount = (Convert.ToInt32(txtAmt.Text)/threads);

if (ind != threads)
{

ind++;
if (started == 0)
{
ThreadStart startz = new ThreadStart(mailit);
worker[ind] = new Thread(startz);
}

worker[ind].Start();

if (ind == (threads-1))
{


}

}

mail.Mail();
}

private void button1_Click(object sender, System.EventArgs e)
{
//to do
//amount sent fix
//add waitOne to the socket class not in the mail class, use timeout
paramater etc
//make this project for exfilter command line
string server;
threads = Convert.ToInt32(txtThreads.Text);
smtp mail2 = new smtp();
if (ind > 0)
{ started=1;
server =
txtTo.Text.Substring(txtTo.Text.IndexOf("@")+1,(txtTo.Text.Length-txtTo.Text.IndexOf("@")-1));
ssrv=mail2.MXLookup(server);
listBox1.Items.Add("00 - Started at "+System.DateTime.Now);
ind=0;
ind++;
worker[ind].Start();
}
else
{

tp.SetMaxThreads(100,5000);



ThreadStart startz = new ThreadStart(mailit);



server =
txtTo.Text.Substring(txtTo.Text.IndexOf("@")+1,(txtTo.Text.Length-txtTo.Text.IndexOf("@")-1));
ssrv=mail2.MXLookup(server);
listBox1.Items.Add("00 - Started at "+System.DateTime.Now);

ind++;
worker[ind] = new Thread(startz);
worker[ind].Start();

}
 
Back
Top