Breno Almeida
Breno Almeida

Reputation: 58

Calling New Thread on Method is a Bad idea?

I'm working on a project that shows, reads(using speech) and mails notifications for the user.

Everything is working, but i have one question that I cant find the answer.

Every time I need to call the speaker class i need to create a new thread. Actually, I'm using a lot of threading to do background work and update some infos.

My doubt:

calling a new thread like this:

public void relatmailth()
{
    MAILRELAT mails = new MAILRELAT();
    relatmail = new Thread(new ThreadStart(mails.mail));
    relatmail.IsBackground = true; 
    relatmail.Start();
}

every time I need to send a email, is a bad practice?

The Program will manage threads automatically?

I need to "kill"(I know it's not recommended) old threads before calling the new one?

if it is, how I can fix that?

Upvotes: 1

Views: 410

Answers (2)

Eric Lippert
Eric Lippert

Reputation: 660289

Everything is working

... by accident, probably.

Every time I need to call the speaker class i need to create a new thread. Actually, I'm using a lot of threading to do background work and update some infos.

That is a very bad practice. Think of threads as workers. If you would not hire a worker to do a task, do not hire a thread to do a task.

every time I need to send a email, is a bad practice?

Would you hire a new admin assistant every time you wanted to send a letter, and then fire them?

Yes, it is a bad practice.

I need to "kill"(I know it's not recommended) old threads before calling the new one?

Why are you asking a question when you know the answer? Never kill a thread. It is incredibly dangerous. If you hire a new admin assistant every time you have a letter to put in the mail, do you shoot them when the mail goes in the mailbox?

The CLR guarantees that its invariants are maintained when you abort a thread. The CLR does not guarantee that your program invariants are maintained when you abort a thread. If you are aborting a thread, you should be aborting every thread and shutting down the process in an emergency because you are worried that running threads will corrupt user data. A fail-fast is the safest way to shut down in this emergency situation.

If you are not in an emergency shutdown situation, do not kill threads.

if it is, how I can fix that?

  • All I/O tasks -- sending emails, connecting to databases, reading and writing files, connecting to the network, and so on -- should be done with single-threaded asynchrony.
  • All CPU-bound tasks should be either farmed out to a separate process, or assigned a worker thread using the appropriate high-level task scheduling technology, such as the TPL. Do not manage your own threads. Let the TPL manage your threads, and only assign it CPU-bound tasks.

Upvotes: 8

Rahul
Rahul

Reputation: 77896

Yes it's a bad idea since you are creating a thread every time you are calling the send email method. Rather either make it a async one like

public async Task relatmailth()
{
    MAILRELAT mails = new MAILRELAT();
    // rest of mail sending logic
}

Else if you really have to have this method schedule on a separate thread, then use one from ThreadPool using QueueUserWorkItem() like

ThreadPool.QueueUserWorkItem(relatmailth, "TP Thread");

Upvotes: 1

Related Questions