Click here to Skip to main content
15,890,185 members
Home / Discussions / C#
   

C#

 
GeneralRe: Waiting for thread to exit? Pin
PIEBALDconsult8-Nov-14 17:32
mvePIEBALDconsult8-Nov-14 17:32 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer018-Nov-14 17:39
SledgeHammer018-Nov-14 17:39 
GeneralRe: Waiting for thread to exit? Pin
Dave Kreskowiak8-Nov-14 18:59
mveDave Kreskowiak8-Nov-14 18:59 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer018-Nov-14 19:20
SledgeHammer018-Nov-14 19:20 
GeneralRe: Waiting for thread to exit? Pin
Dave Kreskowiak9-Nov-14 4:43
mveDave Kreskowiak9-Nov-14 4:43 
GeneralRe: Waiting for thread to exit? Pin
Eddy Vluggen9-Nov-14 5:59
professionalEddy Vluggen9-Nov-14 5:59 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer019-Nov-14 7:31
SledgeHammer019-Nov-14 7:31 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer019-Nov-14 7:28
SledgeHammer019-Nov-14 7:28 
Thank you for your attempt at an explanation of this bizarre behavior, but unfortunately, its not quite accurate Smile | :) . I will show you why with two further examples Smile | :) .

First off, we can all agree that Worker is a foreground thread and that it keeps the process alive regardless of what the main threads state is, right?

When the PROCESS exits, it will abort any BACKGROUND threads abruptly. We can all agree on that too.

Since Worker is blocking the process from exiting, there is no reason for any background threads to get killed since the process is still alive.

EXAMPLE #1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
	class Program
	{
		static bool b = false;

		static void Main(string[] args)
		{
			Thread threadCurrent = Thread.CurrentThread;

			Task.Run(() =>
			{
				while (threadCurrent.IsAlive)
				{
					System.Diagnostics.Debug.WriteLine("MAIN THREAD IS ALIVE");
					Console.WriteLine("MAIN THREAD IS ALIVE");
					Thread.Sleep(250);
				}

				System.Diagnostics.Debug.WriteLine("MAIN THREAD IS DEAD");
				Console.WriteLine("MAIN THREAD IS DEAD");

				b = true;
			});

			Thread _threadWorker = new Thread(new ThreadStart(Worker));

			_threadWorker.SetApartmentState(ApartmentState.STA);
			_threadWorker.Start();

			Thread.Sleep(5000);
		}

		private static void Worker()
		{
			while (true)
			{
				Thread.Sleep(100);

				if (b)
				{
					Console.WriteLine("2) MAIN THREAD IS DEAD");
					break;
				}
			}
		}
	}
}


With this example, we can clearly see that the Task is not getting killed (or at least it appears that way) because everything works as expected. You get "MAIN THREAD IS ALIVE" messages for 5 seconds and then you get a "MAIN THREAD IS DEAD" message AND the "2) MAIN THREAD IS DEAD" message from the worker thread. Everything worked as expected and all messages were printed as expected and everything exited cleanly.

A race condition perhaps? Perhaps... Perhaps not Smile | :) . I've never gotten any of the messages not to print with this implementation, but lets bump up the 250ms sleep to 3000ms and try that...

Nope... still works as expected, everything behaves correctly Smile | :) .

EXAMPLE #2: This is my favorite one Smile | :) ... I'm just replacing the Task code from the code above with this Task implementation:

C#
Task.Run(() =>
{
    //while (threadCurrent.IsAlive)
    //{
    //  System.Diagnostics.Debug.WriteLine("MAIN THREAD IS ALIVE");
    //  Console.WriteLine("MAIN THREAD IS ALIVE");
    //  Thread.Sleep(3000);
    //}

    System.Diagnostics.Debug.WriteLine("WAITING FOR MAIN THREAD TO DIE");
    Console.WriteLine("WAITING FOR MAIN THREAD TO DIE");
    threadCurrent.Join(6000);
    System.Diagnostics.Debug.WriteLine("MAIN THREAD IS DEAD");
    Console.WriteLine("MAIN THREAD IS DEAD");

    b = true;
});


Yes, a contrived example, but it clearly proves that the Task is not getting killed outside of the debugger since after the 6s timeout, the Task is still alive Smile | :) .

Any ideas? I will try using a foreground thread for the watch dog, but your original explanation doesn't explain what we're seeing with these two examples Smile | :) .

I certainly would NOT expect a task to get taken out by the main thread exiting. Background threads only get killed when there are no more foreground threads. There is one in this case: the Worker.
GeneralRe: Waiting for thread to exit? Pin
PIEBALDconsult9-Nov-14 5:59
mvePIEBALDconsult9-Nov-14 5:59 
GeneralRe: Waiting for thread to exit? Pin
Dave Kreskowiak9-Nov-14 7:08
mveDave Kreskowiak9-Nov-14 7:08 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer019-Nov-14 7:48
SledgeHammer019-Nov-14 7:48 
GeneralRe: Waiting for thread to exit? Pin
Dave Kreskowiak9-Nov-14 10:31
mveDave Kreskowiak9-Nov-14 10:31 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer019-Nov-14 11:18
SledgeHammer019-Nov-14 11:18 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer019-Nov-14 7:37
SledgeHammer019-Nov-14 7:37 
GeneralRe: Waiting for thread to exit? Pin
PIEBALDconsult9-Nov-14 7:54
mvePIEBALDconsult9-Nov-14 7:54 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer019-Nov-14 7:57
SledgeHammer019-Nov-14 7:57 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer019-Nov-14 8:00
SledgeHammer019-Nov-14 8:00 
GeneralRe: Waiting for thread to exit? Pin
PIEBALDconsult9-Nov-14 8:13
mvePIEBALDconsult9-Nov-14 8:13 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer019-Nov-14 8:49
SledgeHammer019-Nov-14 8:49 
GeneralRe: Waiting for thread to exit? Pin
PIEBALDconsult9-Nov-14 8:55
mvePIEBALDconsult9-Nov-14 8:55 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer019-Nov-14 9:17
SledgeHammer019-Nov-14 9:17 
GeneralRe: Waiting for thread to exit? Pin
Eddy Vluggen10-Nov-14 8:34
professionalEddy Vluggen10-Nov-14 8:34 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer0110-Nov-14 9:27
SledgeHammer0110-Nov-14 9:27 
GeneralRe: Waiting for thread to exit? Pin
Eddy Vluggen11-Nov-14 0:31
professionalEddy Vluggen11-Nov-14 0:31 
AnswerRe: Waiting for thread to exit? Pin
Garth J Lancaster8-Nov-14 17:31
professionalGarth J Lancaster8-Nov-14 17:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.