Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to accomplish something like this:

 

{
  string sType ="Update";
  Thread th1 = new Thread (new ThreadStart(Operation1(sType)));
  th1.Start();
}

private void Operaton1(string sType)
{
  if (sType == "Update")
  {
  }
  else
  {
  }
}

 

Obviouslly this doesn't work and returns and error message as follows:

Method name expected

 

It works fine if my "Operation1" has no parameters but I somehow need to pass the information from "sType" into the Thread itself...

How could I accomplish passing a STRING into a THREAD I am about to start?

 

Any help would be greatly appreciated.

Thanks,

  • Leaders
Posted

I found one answer to this question in the intellisense that popped up when I typed new Thread(. Assuming it won't be a problem to change the signature of Operation1, try this:

 

C# Code[HorizontalRule]magic hidden text[/HorizontalRule]{

···string sType ="Update";

···// Create a thread that accepts a parameter

···Thread th1 = new Thread (

······new ParameterizedThreadStart(Operation1));

···// Execute thread with specified parameter

···th1.Start(stype);

}

// method signature has been changed

private void Operaton1(object oType)

{

···// String comparison has been modified

···if ("Update".Equals(oType))

···{

···}

···else

···{

···}

}[HorizontalRule]Why are you quoting me?[/HorizontalRule]

 

Also note that the object passed to the thread doesn't have to be a string--it could be an object that specified any kind of data you'd like. You could even tailor a class that the invoked thread can write data back to in order to return data from a thread.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...