How to manage threads in C #?

0

I'm developing a project and I think I'll need multithreading .

While starting a new thread does not seem difficult, I can not seem to figure out if I have to finish the thread or it ends alone.

My application will, at certain times, make one, or several jobs that may take some time. Since I did not want to postpone the process, since one of the most important parts of the project is the "agenda", I thought about doing the various jobs in threads . So, even if a job is taking longer, the main application would be available to run other jobs at the same time.

Once the work that was being done on the thread ends, do I need to destroy the thread ? Or does it, when it's done, destroy itself?

Because of my concern. The program is supposed to run for a long time, days at a minimum and if you do not manage this process the stability of the program would be concerned.

    
asked by anonymous 01.02.2018 / 10:18

2 answers

2
  

I'm developing a project and I think I'm going to need multithreading.

Then see this to make sure you really need it. Most people believe that thread is something magical that makes a code run faster.

  

While starting a new thread does not seem difficult, I can not seem to figure out if I have to finish the thread or it ends alone.

It's a special class that manages itself and does not have to finish it.

  

My application will, at certain times, do one or several jobs that may take some time. Since I did not want to postpone the process, since one of the most important parts of the project is the "agenda", I thought about doing the various jobs in threads. So, even if a job is taking longer, the main application would be available to run other jobs at the same time.

Something tells me you do not need thread . Or you can do it in process alone or you can use asynchronicity . But you can not say without understanding the need.

  

Once the job that was being done on the thread ends, do I need to destroy the thread? Or does it, when it's done, destroy itself?

No need to worry, even if you use Task which is the most correct, Thread . it's just to build things down. But that's the easy part of thread , managing multiple threads is absurdly more complicated than knowing if it should destroy it.

Note that I am not saying that it is destroyed.

  

Because of my concern. The program is supposed to run for a long time, days at a minimum and if you do not manage this process the stability of the program would be concerned.

Yes, it looks like you need an external service.

See Are there any differences between the terms Thread, Multithread, Async, and Await? .

    
01.02.2018 / 11:40
0
  • Threads are finalized when execution is finished. You do not have to worry about it.

  • The biggest concern with multithreaded operations is usually sync control. Eventually you will have several operations in parallel and in the end you would like the resultant of these operations to be executed on a third thread that should be executed only after the completion of the primary threads, so that Task.Wait ( link ). You fire all the threads and at the end you wait for the completion so only (when all is completed) proceed with the process.

  • Another important concern is the manipulation of data with the possibility of deadlook. To do this, use the Lock ( link ) so that only one of the threads will execute the operation inside the Lock at a time, allowing synchronization in that environment.

01.02.2018 / 11:08