none
Background Thread is not terminated when Foreground Threads are terminated.

    Question

  • Hi!

    I have been trying the example shown in this link:

    I am sorry! I can´t post links!!! The webpage is in MSDN:

    Thread.IsBackground Property

    However, I have noticed that this example does not work the same as it is said.

    Both Threads complete their task. The background thread goes up to iteration 50...even when the foreground thread is terminated.

    In theory the background thread should be terminated as soon as all foreground thread are terminated. Right?

    Am I missing something?

    Thank you very much!!

    Thursday, March 23, 2017 1:38 PM

Answers

  • Wrong is a very strong word here - I'm putting the code into a console application / project and running it.

    If you're putting it in a forms application you have a UI thread that is alive  after the foreground and background thread complete. In a console application, when the foreground thread completes the program terminates. This isn't the case in a forms application.

    The only bit of the puzzle you were missing was that the Form had this other thread, so the process didn't terminate when the foreground thread completed.

    Make sense?

    Regards,

    D

    • Proposed as answer by bowman_d Thursday, March 23, 2017 9:38 PM
    • Marked as answer by vanerio Thursday, March 23, 2017 10:48 PM
    Thursday, March 23, 2017 9:38 PM

All replies

  • Hi,

    If you look at the code, the run loop has a sleep of 250 - which both threads use. That is more that sufficient time for the background thread to finish its work before the foreground thread terminates.

    D

    • Proposed as answer by bowman_d Thursday, March 23, 2017 1:48 PM
    Thursday, March 23, 2017 1:48 PM
  • In this is always the reporting tool important. 

    If it is 100% busy with reporting what runs, they simply everything can stop running and report tool can report  0% is busy because the report tool has taken all the process. 

    If the background thread is still running after 2 minutes than you can think of something which does not go likewise you wanted. Therefore tell us the interval which you use to view at this. 


    Success
    Cor

    Thursday, March 23, 2017 1:49 PM
  • Dear friend, this is simply not true.

    Please try it on your own and you will see that the background thread continues after the foreground thread is terminated. (At least in my case). You can check it in the Console.

     

     

    Thursday, March 23, 2017 1:58 PM
  • I was testing the exact same example shown in MSDN

    Thursday, March 23, 2017 2:12 PM
  • Hi,

    As Cor suggests you need to be careful in respect of how you are determining that the threads are running. The console output will take longer than the entire process takes, so you could easily be convinced that threads are still running when in fact you are merely seeing buffered output.

    The ultimate test is whether the process is completely finished or whether it remains in the task manager after you think its completed. If you can still see an instance of the process in task manger after it should have finished, you have a rogue thread that hasn't finished.

    If you want to test the issue completely, set the background task to count to 50,000 rather than 50 you'll see that the thread is actually terminated long before it gets to finish.

    Regards,

    D

    Thursday, March 23, 2017 3:47 PM
  • I actually did it, and it continues to the end...

    I will say it again. I have just copy-pasted the example from MSDN.

    As they use the SAME Sleep (250), they must be terminated like MSDN says:

    '  Foreground Thread count: 10
    '  Background Thread count: 11
    '  Foreground Thread finished counting.

    But it is not my case! It continues counting till maxIterations is reached!

    '  Foreground Thread count: 10
    '  Background Thread count: 11
    '  Foreground Thread finished counting.
    '  Background Thread count: 11
    '  Background Thread count: 12
    '  Background Thread count: 13
    '  Background Thread count: 14
    '  Background Thread count: 15
    '  Background Thread count: 16
    '  Background Thread count: 17
    '  Background Thread count: 18
    '  ...
    '  ...
    '  ...

    Thank you for your time!!

    Thursday, March 23, 2017 4:15 PM
  • Interesting - what environment are you running this on?

    Are you running it within Visual Studio or directly from the command line?

    I've tried this code unmodified on both a PC via Visual Studio and on a Mac via Xamarin and got exactly the same results both times - background thread killed as soon as the foreground finishes.

    Regards,

    D

    Thursday, March 23, 2017 4:28 PM
  • vanerio,

    You're probably better off to use something that manages all of that for you:

    Task-based Asynchronous Pattern (TAP)

    Food for thought. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, March 23, 2017 5:03 PM
  • I am running it withing Visual Studio 2015 in Windows 8

    Sorry, I can´t upload images to show it!!

    (I can´t install VS 2017 because it says I have not Admin privileges, what is false...so strange...)

    Thanks!!

    Thursday, March 23, 2017 5:04 PM
  • OK that's certainly curious - I've run it on VS2015 on Windows 10 and it does exactly what MSDN suggests.

    Try running the .exe from the command line and see what the results are.

    Regards,

    D

    Thursday, March 23, 2017 5:39 PM
  • Mmmm. I created a Form and a Button which was the one that was calling: Example.Main()

    That is the case that counts until maxIterations is reached!

    Am I doing something wrong?

    Shouldn´t a background thread be terminated in this case??

    Thank you!!!

     

    Thursday, March 23, 2017 7:20 PM
  • Wrong is a very strong word here - I'm putting the code into a console application / project and running it.

    If you're putting it in a forms application you have a UI thread that is alive  after the foreground and background thread complete. In a console application, when the foreground thread completes the program terminates. This isn't the case in a forms application.

    The only bit of the puzzle you were missing was that the Form had this other thread, so the process didn't terminate when the foreground thread completed.

    Make sense?

    Regards,

    D

    • Proposed as answer by bowman_d Thursday, March 23, 2017 9:38 PM
    • Marked as answer by vanerio Thursday, March 23, 2017 10:48 PM
    Thursday, March 23, 2017 9:38 PM
  • Exactly!

    I thought that the foreground threads that I should terminate were the ones that were started in a Sub!!

    Thank you very much to all of you!!!!!!

    Best Regards!

    Thursday, March 23, 2017 10:53 PM