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){
//} 
Doro
  • 671
  • 1
  • 16
  • 34
  • Did you read the documentation? using via Thread class: http://msdn.microsoft.com/en-us/library/6x4c42hc(v=vs.110).aspx . Using ThreadPool (Recommended approach): http://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. – Ahmed ilyas Jul 13 '14 at 11:49
  • You can use `ParameterizedThreadStart` to create parameterized thread. – Shell Jul 13 '14 at 11:49
  • @Ahmedilyas, I read part of the documentation but not all :-( – Doro Jul 13 '14 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. – Doro Jul 13 '14 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. – ilansch Jul 13 '14 at 12:17
  • @ilansch - I am using 4.5 but would be great if could work on both. I prefer create a new thred. I don't know about Task, I have to look into it. – Doro Jul 13 '14 at 12:19

3 Answers3

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

Community
  • 1
  • 1
ilansch
  • 4,784
  • 7
  • 47
  • 96
  • What about the thread finished when the method has completed it's duty? – Doro Jul 13 '14 at 12:23
  • 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 – ilansch Jul 13 '14 at 13:00
  • That is smt that I didn't consider. Do you have any suggestions? – Doro Jul 13 '14 at 13:59
  • 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? – Doro Jul 13 '14 at 17:55
  • 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 – ilansch Jul 14 '14 at 07:08
  • I have to load a gridView with more than 6,000 rows. The function takes too much time and the GUI is not responding while loading. – Doro Jul 14 '14 at 18:05
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();
Tzah Mama
  • 1,547
  • 1
  • 13
  • 25
  • Thank you for the answer. Could you please post where should come var1, var2 and var3? – Doro Jul 13 '14 at 11:52
  • @isidoro var1,var & var3 are just place holders. Use your variable the same way you'd call the function without thread – Tzah Mama Jul 13 '14 at 11:57
  • Do you mean in this way: Thread T1 = new Thread(() => LoadData([dgv], [rb1], [rb2])).Start(); – Doro Jul 13 '14 at 12:01
  • Yes, like that, except for the square brackets. – Lasse V. Karlsen Jul 13 '14 at 12:06
  • @isidoro Sorry if this confused you, this is just my way of formatting. You don't need the square brackets just type your variables – Tzah Mama Jul 13 '14 at 12:06
  • @TzahMama I tries this: Thread T1 = new Thread(() => LoadData(dgv, rb1, rb2)).Start(); but says "Cannot implicitly convert type 'void' to 'System.Threading.Thread'" – Doro Jul 13 '14 at 12:12
  • @isidoro If you are assigning the thread to a variable you can't start it. Change your code to `Thread T1 = new Thread(() => LoadData(dgv,rb1,rb2));` and then `T1.Start();` – Tzah Mama Jul 13 '14 at 12:14
  • That's great, it is working! Do you know if Regular Expression is the only way to solve it? – Doro Jul 13 '14 at 12:17
  • 1
    @isidoro Lambda expressions are not the same as Regular expressions :). And yes, you can use [ParameterizedThreadStart](http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart(v=vs.110).aspx) but it's a lot faster and cleaner using lambda – Tzah Mama Jul 13 '14 at 12:19
  • @TzahMama - BTW, I meant Lambda Expression and not Regular xpression! :-( – Doro Jul 13 '14 at 12:41
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));
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321