简体   繁体   中英

C# winforms multithread on method with parameters

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){
//} 

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

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();

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM