How to manage threads in C#?

Asked

Viewed 161 times

0

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

While starting a thread new doesn’t seem difficult, I can’t understand if I have to finish the thread or she ends up alone.

My application will, in certain hours make a, or several, jobs that may take some time. As I did not want to postpone the process, since one of the most important parts of the project is the "agenda", I thought to do the various works 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 was being done on thread ends, I need to destroy the thread? Or she herself, when she’s done, destroys herself?

Why I’m so worried. The programme is supposed to run for a long time, days at least and if it does not manage this process the stability of the programme would be at stake.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

2

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

So 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 doesn’t seem difficult, I can’t figure out if I have to finish the thread or it ends alone.

It is a special class that manages itself and does not need to finish it.

My application will, in certain hours, do one or several jobs that may take some time. As I did not want to postpone the process, since one of the most important parts of the project is the "agenda", I thought of doing the various work 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 don’t need thread. Or you can do in process isolated or can use asynchronicity. But you can’t say without understanding the need.

Once the work that was being done on the thread ends, do I need to destroy the thread? Or does she herself, when finished, destroy herself?

No need to worry, especially if you use Task which is the right thing to do, Thread is just to build things on a low level. But that’s the easy part of thread, manage multiple threads is absurdly more complicated than whether to destroy it.

Note that I am not saying that it is destroyed.

Why I’m so worried. The programme is supposed to run for a long time, days at least and if it does not manage this process the stability of the programme would be at stake.

Yeah, looks like you need an outside service.

Behold There are differences between the terms Thread, Multithread, Async and Await?.

  • Thank you for the comprehensive reply. At this moment I am testing the application and realized that when the process involving the thread is executed, there is a jump in RAM consumption (from 10mb to 60mb) and if there are 2 processes (from 10 to ~200mb) which, in my case, cannot be accepted. Can you tell me if there’s a reason this might be thread-related? EDIT: Even after the process is over, consumption does not drop.

  • It’s likely, but you can’t know so superficially.

  • Can I help? Soon, the application, serves to make backups. It also has a small "task programmer". Once it comes to a certain time, it backs up. As the backup process takes some time, it cannot be done in main. I then started exploring ways to back up without interrupting the main application. Considering that the amount of backups at the same time is undefined precise a way to do various jobs at the same time without interrupting the life of the main application.

  • Nor should do in the same process, thread is for something else.

0

  • Threads are finished when finishing the execution. You don’t have to worry about that.

  • The major concern with multithread operations is usually timing control. Eventually you will have several operations in parallel and in the end you would like the result of these operations to be executed in a third thread that must be executed only after the completion of the primary threads, for this can be used the Task.Wait (https://msdn.microsoft.com/pt-br/library/dd235635(v=vs.110). aspx). You fire all the threads and in the end wait for the conclusion to only then (when finishing all), follow the process.

  • Another important concern is the manipulation of data with deadlook possibility. To do this, we use the Lock (https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/keywords/lock-statement) so that only one of the threads will perform the operation inside the Lock at a time, allowing a synchronization in this environment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.