0

Hello I am building a C# application and I have few questions regarding threads.

First of all I want to open threads in functions that has some arguments. I have the following code for example:

Thread GetStaff= new Thread(() => GetStaff(arg1));
GetStaff.Name = "Get Staff";
GetStaff.Start();
main_threads.Add(GetStaff);

Is the above code the best I can use to open a thread? Also the GetStaff.Name is always right to using it to assign a name to my thread?

The main_threads in the above code is just a simple list. It is ok to have all the threads saved in a list and list them using this? Or its better to have dynamic code to list the threads?

I use the following code to close the threads:

foreach (Thread thread_work in main_threads)
{
     if (thread_work.IsAlive)
     {
          thread_work.Abort();
          thread_work.Join();    
     }
}
main_threads.Clear();

Any improvements?

Kristof U.
  • 1,263
  • 10
  • 17
  • 1
    Not enough context. Why are you trying to keep tabs of all your threads like this, and stopping them all later? – sethcall Nov 24 '13 at 13:42
  • 4
    `Abort` is something which you should never be using in your code. Take a look at `TPL` and "Cooperative cancellation pattern". – Sriram Sakthivel Nov 24 '13 at 13:49
  • Because the threads are doing a job that takes time. It may needed to stop the threads if i want to, before doing their job. – user3027324 Nov 24 '13 at 13:54
  • @user3027324 Abort does not "Stop" the thread it destroys it, maybe while doing something that should not be interrupted. Abort can cause Problems with corrupted Files / Databases, lost Handles and so on. Have you tried using Tasks instead? – quadroid Mar 06 '18 at 08:04

1 Answers1

1

Don't use a list when you're using multiple threads, use concurrent collections, because they're designed for thread-safety, where as a normal generic list is not.

http://msdn.microsoft.com/en-us/library/system.collections.concurrent.aspx

Also GetStaff is not convenient as a name for a thread. You could call it something like getStaffThread or StaffThread, but it's not the thread that get's "staff" it's the function(s) executed within the thread.

Also:

Thread GetStaff= new Thread(() => GetStaff(arg1));

You're basically creating a thread with a delegate parameter that calls the constructor of the same thread with a parameter called arg1, however this won't even compile.

I assume it's just a mistake that you named both the thread and your function GetStaff? Or is the case really that you're trying to do what I described above?

Anyways in either case there is an overload of Thread() with a delegate that takes a parameter.

http://msdn.microsoft.com/en-us/library/1h2f2459.aspx

Then you could do something like.

// GetStaff thread ...
Thread getStaffThread = new Thread(GetStaff);
// ...

// GetStaff function ...
static void GetStaff(object o)
{
    // Do stuff ...
}

I think that's all I got.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Bauss
  • 2,767
  • 24
  • 28
  • Oh ok i understand everything regarding this. And my last question how to properly stop a thread? Someone said Abort should not be used – user3027324 Nov 24 '13 at 14:09
  • You may want to look here http://stackoverflow.com/questions/1051838/killing-a-thread-c – Bauss Nov 24 '13 at 14:19
  • @user3027324 Yes, and they also provided you with a term you can easily lookup and read about it. – BartoszKP Nov 24 '13 at 14:19