views:

154

answers:

4

Hello Experts,

I am building a C# Desktop application. How do I call a method that takes multiple parameters in a thread. I have a method called Send(string arg1, string arg2, string arg3) , I need to call this method using a thread called SendingThread. Can anyone help out with this? Any help will be much appreciated.

+2  A: 

You could define a type, that encapsulates the parameters you want to pass and start the thread with a reference to an instance of this type.

Brian Rasmussen
When down voting, please leave a comment. Thanks.
Brian Rasmussen
good answer but what about aborting said thread ?
f00
f00: Not sure I follow you here. What do you mean? There's nothing special about a thread that is started with a reference to an object.
Brian Rasmussen
A: 

You can define an intermediate method and helper object to do this:

public void MethodToCallInThread(string param1, string param2)
{
    ...
}
public void HelperMethod(object helper){
    var h = (HelperObject) helper;
    MethodToCallInThread(h.param1, h.param2);
}

And then you start the thread with the HelperMethod, not with MethodToCallInThread:

var h = new HelperObject { param1 = p1, param2 = p2 }
ThreadPool.QueueUserWorkItem(HelperMethod, h);
Ronald Wildenberg
Can someone please explain the -1??
Ronald Wildenberg
i got the same :). it is common stupid situation...i like red color but other one likes yellow :) and i hate all NON red-funs.
igor
I suspect the -1 is for using an ancient style of passing parameters that is really quite cumbersome compared to the new style available in modern versions of C#.
romkyns
I understand there are better ways to do this. Timwi's answer obviously being the best solution. However, the answer itself isn't wrong so I suspect this was a tactical downvote (especially because of the lack of any comment why it was downvoted by the voter). Besides, I think this is a better solution than the accepted one.
Ronald Wildenberg
I can’t speak for the downvoter, but personally I don’t always comment when I downvote, and I don’t think anyone should be compelled to. As for the accepted answer, I did downvote that and commented on it too. (As a sidenote, now that you realise that there *is* such a thing as a “tactical downvote”, I recommend you resent StackOverflow, which establishes the stupid rules, not the downvoter who simply plays the game according to the stupid rules.)
Timwi
@Timwi I usually do comment on a downvote. But I usually downvote to point out a technical incorrectness and it's only fair to point out what the incorrectness is. I do not resent SO, although I wish they would come up with some way to prevent tactical downvoting (although, having given some thought about it, I think this is impossible without sacrificing on other aspects). I do resent tactical downvoting itself, however. It's a very cheap and dishonest way to advertise your own answer. Please let others decide what answer is best. I actually upvoted your answer because it's better.
Ronald Wildenberg
Just to avoid misunderstandings here: I don’t like tactical downvoting either. All I’m saying is that *StackOverflow encourages it, therefore don’t be surprised that people do it.* It’s like giving someone money for free and then criticising them for spending it. If you resent tactical downvoting, the only rational course of action is to campaign against SO’s rules, not against tactical downvoting.
Timwi
Don't worry, there's no misunderstanding :) But I don't agree that SO encourages it. SO allows it but everyone has a choice. If you play a card game and someone accidentally shows you their hand, it's your choice to look at the cards. Your opponent allows you to cheat but doesn't encourage it. But this is turning into some moral discussion so maybe we'd better stop here ;-)
Ronald Wildenberg
A: 

Hi, this may help. You can define your Send method as follows and then can use the parameters.

string[] parameters = new string[3];
parameters[0] = arg1;
parameters[1] = arg2;
parameters[1] = arg3;

System.Threading.Thread SendingThread = new System.Threading.Thread(Send);
SendingThread.Start(parameters);


public void Send(object parameters)
{
   Array arrayParameters = new object[3];
   arrayParameters = (Array)parameters;
   string str1 = (string)arrayParameters.GetValue(0);
   string str2 = (string)arrayParameters.GetValue(1);
   string str3 = (string)arrayParameters.GetValue(2);

   ///Following code here...
}
sumit_programmer
① This unnecessarily removes all type safety. (Unnecessarily because you could just pass the strings instead of an object.) ② Using `Array` is really weird. Why not cast to `string[]` directly? ③ Why are you allocating a `new object[3]` and then immediately obsolete it again? That’s redundant. ④ What is `ReceiveMsgThread`? You haven’t declared that anywhere. ⑤ What does this answer do that mine doesn’t (other than destroying type-safety)?
Timwi
As highlighted by Timwi, this code has numerous issues despite being the accepted answer. Please don't use this.
romkyns
@Timwi: 1st of all yes instead of ReceiveMsgThread it should be SendingThread. I am sorry about that. Secondly, I have taken it as an array because you can't cast an object type to string[] directly. I have passed string[] in the argument parameters, how would you take out separate strings from object parameters. You need to cast it as an array and extract the values from that array casting it as string. Why don't you just give it a try and check if that's possible.And also in the object parameters, we can pass string, int, so on different types and then extract the corresponding types later.
sumit_programmer
@sumit-programmer: You can cast `object` to `string[]` just fine. I’m sorry, but you clearly have no clue. Besides, there is no point in your answer because mine does *exactly the same thing*, but in one line, *and* in a type-safe manner.
Timwi
@Timwi: Yes, you're right, we can cast object to string[]. My main idea behind taking as array is to its not type limited, like if string, int, or any other different types are passed in parameters, Array can take care of that. So, we can pass different data types in parameters.And yes, your method may be shorter, haven't tried that though.
sumit_programmer
+11  A: 
Thread thread = new Thread(() => Send(arg1, arg2, arg3));
thread.Start();
Timwi
@prateeksaluja20: I’ve edited this answer to use your method name. Is this clearer?
Timwi
looks good to me, alternatively you could wrap the whole sendthread functionality up in it's own class http://paste.pocoo.org/show/256213/
f00
@f00 why take the time and effort to create a closure class when the compiler does it for you?
Will
because i want to encapsulate all of the SendThread functionality in it's own class with a publishhed interface so it's easy to use and shields the user from the intricacies of creating and using threads in the raw form
f00