Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if an application is closed while a thread of ThreadPool is writing a file?

Suppose you use a ThreadPool to perform some operations and assume that each operation writes on a file. All threads of ThreadPool are background threads, so they will be terminated when closing the application. What happens if the application is closed while a thread of ThreadPool is writing a file to disk?

like image 228
enzom83 Avatar asked Dec 20 '22 06:12

enzom83


2 Answers

Operating system will close the file handle as the part of terminating the process.

  • Any pending asynchronous I/O will be canceled
  • Any data in the write buffer which isn't flushed gets lost
like image 50
Sriram Sakthivel Avatar answered Feb 15 '23 23:02

Sriram Sakthivel


Read the MSDN article on Foreground and Background threads

ThreadPool threads are background threads. From the article:

When the runtime stops a background thread because the process is shutting down, no exception is thrown in the thread.

The thread simply stops. It executes one instruction, and never executes the next. The FileStream will be closed as part of the CLR clearing up.

like image 31
canton7 Avatar answered Feb 15 '23 23:02

canton7