0

I have a method with 3 parameters on which I would like to create for it a thread. I know how to create a thread for a method without any paremeters and with object type parameter. The method header is:

public void LoadData(DataGridView d, RadioButton rb1, RadioButton rb2){
//} 
6
  • Did you read the documentation? using via Thread class: msdn.microsoft.com/en-us/library/6x4c42hc(v=vs.110).aspx . Using ThreadPool (Recommended approach): msdn.microsoft.com/en-us/library/4yd16hza(v=vs.110).aspx . your "state" parameter is the parameters you are wanting to pass to it. you cast (for both approaches) objects to the actual desired object. Commented Jul 13, 2014 at 11:49
  • You can use ParameterizedThreadStart to create parameterized thread. Commented Jul 13, 2014 at 11:49
  • @Ahmedilyas, I read part of the documentation but not all :-( Commented Jul 13, 2014 at 11:55
  • @Shell, I don't know much about complex multithread, I know that we can use object type (not safe) and safe which include using the constructor. If you can help me to solve this I would really appreacite. Commented Jul 13, 2014 at 11:57
  • Hey, First of all, What .Net are you using ? if later then 4.0 consider using Task, If earlier, ask yourself, Do you want a thread from threadpool, or to create a new thread that you have to manage (meaning kill) for yourself. Commented Jul 13, 2014 at 12:17

3 Answers 3

2

In addition to Tzah answer, you do not mention the thread life time and management. This is a good place to think about it - As long as you write high quality code..

If you use thread from threadpool with 3 params and more, using my previous answer: C# - ThreadPool QueueUserWorkItem Use?

If you are using .Net 4.0+ consider using Tasks

Sign up to request clarification or add additional context in comments.

6 Comments

What about the thread finished when the method has completed it's duty?
You created Thread class, this is an object, you called a Start, it run some code and finish (stop running the code), the thread class, is still alive, who is disposing it. This can cause serious memory leaks if it hold reference to unmanaged resources or if the invoking class has long-running uptime
That is smt that I didn't consider. Do you have any suggestions?
I want create a new thread or task on a long time taking method, to load data into a gridView. I would like to load data while keeping the UI working properly. Do you still think task would be the best options?
What is long - running operation ? define it. in addition im not client side developer, can you update gridview (GUI) from worker thread ? i think you'll get exception
|
1

You can use Lambda Expression like this:

new Thread(() => LoadData(var1, var2, var3)).Start();

or

Thread T1 = new Thread(() => LoadData(var1, var2, var3));
T1.Start();

10 Comments

Thank you for the answer. Could you please post where should come var1, var2 and var3?
@isidoro var1,var & var3 are just place holders. Use your variable the same way you'd call the function without thread
Do you mean in this way: Thread T1 = new Thread(() => LoadData([dgv], [rb1], [rb2])).Start();
Yes, like that, except for the square brackets.
@isidoro Lambda expressions are not the same as Regular expressions :). And yes, you can use ParameterizedThreadStart but it's a lot faster and cleaner using lambda
|
1

As Tzah's answer will definitely work, the recommanded way of using threads in the .NET Framework now resides with the Task Parallel Library. The TPL provided an abstraction over the ThreadPool, which manages a pool of threads for us to use instead of creating and destroying, which has a non-neglectibale cost. They may not be suitable for all sorts of offload work (like very long running cpu consuming tasks), but they will definitely cover most cases.

An example equivalent to your request using the TPL would be to use Task.Run:

Task task = Task.Run(() => LoadData(var1, var2, var3));

4 Comments

thank you for your answer. My problem is load data into a gridView and the long time taking task should not effect the functionality og the UI.
The task wont effect the responsiveness of your UI.
So, if the LoadData() make the UI non-respond while running, task can help to solve it?
Task.Run will use a ThreadPool thread to execute the delegate. So yes, it will solve this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.