Search code examples
c#.netmultithreadingthreadpoolbackground-thread

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?


Solution

  • 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