Click here to Skip to main content
15,919,774 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to find the nearest number that divides evenly in to another number. Pin
Member 1122095210-Nov-14 23:03
Member 1122095210-Nov-14 23:03 
GeneralRe: How to find the nearest number that divides evenly in to another number. Pin
BillWoodruff11-Nov-14 5:16
professionalBillWoodruff11-Nov-14 5:16 
QuestionAccess network drive though windows services in c# or vc++ Pin
Member 109313459-Nov-14 21:38
Member 109313459-Nov-14 21:38 
AnswerRe: Access network drive though windows services in c# or vc++ Pin
Garth J Lancaster9-Nov-14 21:54
professionalGarth J Lancaster9-Nov-14 21:54 
QuestionDownload youtube with C#2005 ? Pin
Member 24584679-Nov-14 21:20
Member 24584679-Nov-14 21:20 
AnswerRe: Download youtube with C#2005 ? Pin
M.Scheeren10-Nov-14 1:46
professionalM.Scheeren10-Nov-14 1:46 
GeneralRe: Download youtube with C#2005 ? Pin
Member 245846722-Nov-14 20:21
Member 245846722-Nov-14 20:21 
GeneralRe: Download youtube with C#2005 ? Pin
M.Scheeren23-Nov-14 19:57
professionalM.Scheeren23-Nov-14 19:57 
GeneralRe: Download youtube with C#2005 ? Pin
Member 245846726-Nov-14 20:09
Member 245846726-Nov-14 20:09 
GeneralRe: Download youtube with C#2005 ? Pin
M.Scheeren1-Dec-14 22:31
professionalM.Scheeren1-Dec-14 22:31 
QuestionHow to find duplicate Value from Datagridview Pin
Member 107170859-Nov-14 16:27
Member 107170859-Nov-14 16:27 
AnswerRe: How to find duplicate Value from Datagridview Pin
Richard MacCutchan9-Nov-14 21:48
mveRichard MacCutchan9-Nov-14 21:48 
QuestionWaiting for thread to exit? Pin
SledgeHammer018-Nov-14 17:07
SledgeHammer018-Nov-14 17:07 
GeneralRe: Waiting for thread to exit? Pin
PIEBALDconsult8-Nov-14 17:19
mvePIEBALDconsult8-Nov-14 17:19 
GeneralRe: Waiting for thread to exit? Pin
SledgeHammer018-Nov-14 17:24
SledgeHammer018-Nov-14 17:24 
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 

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.