Shaitan00 Posted July 20, 2007 Posted July 20, 2007 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, Quote
Leaders snarfblam Posted July 20, 2007 Leaders Posted July 20, 2007 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.