Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a thread close automatically?

Im using a gmail class so that my app can send me notification over gmail.

Its done like this:

public static void SendMessage(string message) {     Notification.message = message;      Thread t = new Thread(new ThreadStart(SendMessageThreaded));     t.Start(); } 

and the threaded function look like this:

private static void SendMessageThreaded() {     try     {         if (Notification.message != "")             RC.Gmail.GmailMessage.SendFromGmail("accname", "accpass", "email", "subject", Notification.message);          Notification.message = "";     }     catch     { } } 

So after SendMessageThreaded is run, does it close by itself or do i have to

t.Start() t.Abort() 

or something?

like image 628
Jason94 Avatar asked Dec 28 '10 07:12

Jason94


People also ask

Do threads end automatically?

A thread is automatically destroyed when the run() method has completed. But it might be required to kill/stop a thread before it has completed its life cycle. Previously, methods suspend(), resume() and stop() were used to manage the execution of threads.

Can a thread exits without a process?

A single thread can exit in three ways, thereby stopping its flow of control, without terminating the entire process. The thread can simply return from the start routine. The return value is the thread's exit code.

How do threads end?

Whenever we want to stop a thread from running state by calling stop() method of Thread class in Java. This method stops the execution of a running thread and removes it from the waiting threads pool and garbage collected. A thread will also move to the dead state automatically when it reaches the end of its method.

How do you know when a thread has finished?

Use Thread. Join(TimeSpan. Zero) It will not block the caller and returns a value indicating whether the thread has completed its work.


1 Answers

The thread needs to be started once - at which point it will execute the code block assigned to it and exit.

You don't need to explicitly clean up the thread in most cases (unless you want to bail out early for example )

like image 55
Gishu Avatar answered Oct 09 '22 03:10

Gishu