0

So, I basically have this:

public void DoThisThing()
{
    Task.Run(() =>
    {
        while(true)
        {
            //Do things
        }
    }
}

The start of the application basically calls the DoThisThing() method and enters it's own loop.

So, if I just close the application, what happens to this task? Does it just end? does it continue forever? Does it go on for a little bit until garbage collection gets it? Does it have a way to know the application ended?

I googled, but I couldn't get a simple answer, and I feel like there definitely is one.

user1539405
  • 111
  • 1
  • 14

4 Answers4

0

All which is said below is implementation details - FOR WINDOWS - and mostly undocumented behavior. Do not rely on any of the information.

As an implementation detail, this task will most likely be scheduled to execute on a thread pool thread.

If the task has not started by the time the process exit starts, it won't matter it was queued in the first place.

If the task is currently executing, then according to some of the implementation details of process shutdown on Windows eventually only one thread will be executing which will not be the one executing this task. So, it will be forcibly terminated in that case.

If the task has already finished execution, whether through completion or by throwing an exception then there's no thread occupied by it. However, if the exception was left unobserved then the finalizer - should it get a chance to execute - will throw that. Please note that finalizers are also not guaranteed to execute under any circumstances.

This page should have been visible, but Microsoft's latest screw up in revoking access to old MSDN blogs continues.

Similarly, if you can manage to track the first link on this page then do so and read it.

P.S.: Here's the link for Raymond's blog. What you'll find from both sources is that only one thread continues the process shutdown.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
  • 1
    FYI, Microsoft has been terrible at letting a lot of docs / blog links to rot, however you can often find the snapshots archive at the [wayback machine](https://archive.org/web/web.php) – Pac0 May 09 '20 at 14:14
  • I just tested `Task.Run(() => for(;;);)` and when I close the form the entire application ends with no orphaned threads. At least to me it seems as though all threads are terminated when the application exits. – Chris_F May 05 '22 at 16:54
0

The first question is, how this task is even executed. According to the Documentation:

Queues the specified work to run on the ThreadPool and returns a task or Task handle for that work.

Each programm starts with one Thread, but can start further. This one is the Main Thread, the GUI Thread and a few other names. As a general rule, if that main thread is ending, all others threads it started are canceled too. This is a OS level rule, to avoid Zombie Threads with infinite loops running all over the place.

The ThreadPool and all it's data - including sheduled and running Threads - will be collected by the "End of Application" Garbage Colleciton. If not, there is propably some OS features to make sure they end as well. But I prefer not to mention those OS fallbacks, as we really should not be using them ever. There are for cases when even the GC can no longe run properly (like Programm being killed via Task Manager).

One of the big challenges of Multitasking and -threading is keeping the main Thread alive, but without blocking it so further I/O can happen. In a GUI you have that work done for you (with the EventQueue).

Christopher
  • 9,634
  • 2
  • 17
  • 31
0

The answer depends on the content of the while loop. If the loop is running some logic that runs entirely within the scope and control of the main program, then closing the application will terminate everything.

However, if the loop is calling some external routines or operating system functions (Example: write to a file, open a network connection, run a command, start a batch job, etc), then closing the application will not terminate everything.

Gopinath
  • 4,066
  • 1
  • 14
  • 16
0

Based on your sample, in brief: Yes

Tasks that are created by TPL (using Task.Run or Task.Factory.StartNew) by default are background threads. So closing application will immediately terminate them.

This post could be helpfull.

Arman Ebrahimpour
  • 4,252
  • 1
  • 14
  • 46