1

I'm new to threading, so there are a few things I'm trying to grasp correctly.

I have a windows form application that uses threading to keep my UI responsive while some server shenanigans are going on.

My question is: when I quit my application, what happens to ongoing threads? Will they run to completion or will the abruptly be interrupted?

If they are interrupted, what can I do to make sure they at least don't get interrupted in such a way that would corrupt data on my server (force them to run to a safe place in the code where I know it's ok to interrupt the execution)

user1747281
  • 149
  • 1
  • 13
  • 1
    It depends on the value of Thread.IsBackground. If it is False then the program will not terminate until such a thread runs to completion. If it is True, and no other non-background threads are running, then the thread will be rudely aborted. – Hans Passant Jan 26 '18 at 16:41

2 Answers2

1

A thread needs a process to run in. The process won't be able to terminate if you don't terminate all the non-background threads you have started. Threads marked as background thread will be aborted.

So, the behavior is entirely up to your implementation. If you want to close the application, you could wait for all threads to terminate by themself, you could set an event to ask them to terminate and wait or you could just kill the threads.

The UI thread will terminate by itself because it runs a messageloop that stops when requested by the operating system, also see wikipedia and this answer.

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
1

You will want to keep a reference of said threads, and call .Abort() on them when you want to terminate. Then you put your thread's code in a try/catch block and handle ThreadAbortException's. This will let you clean up what you are doing and terminate the thread cleanly at your own pace. In the main thread, after you called .Abort(), you just wait until the thread is no longer running (by polling the .IsAlive property of the Thread object) and close your application afterwards.

Drunken Code Monkey
  • 1,796
  • 1
  • 14
  • 18
  • Abort on a thread interrupts it by throwing an exception, so that will make it hard to clean up properly. Having the thread check at specific points whether or not it should stop allows the thread to clean up properly. – Emond Jan 26 '18 at 17:02
  • I don't see what difficulty this adds, you just need to catch the exception and do your cleanup... – Drunken Code Monkey Jan 26 '18 at 17:49
  • When the thread at the start of the catch you do not know what the thread was doing exactly, so you do not know how to clean up. See the title of the question: prevent an interruption at a bad time – Emond Jan 26 '18 at 18:03
  • I disagree here, because hopefully you place your try catch block around an area within which you know *exactly* what it is doing. That is the whole point of this mechanism. If the code in the try block is doing data insertion for example, then catch the exception, set a boolean flag, and terminate after your next iteration or whatever. Of course if you have 200 lines of code within a single try block, you will have other stuff to correct before this works right. – Drunken Code Monkey Jan 26 '18 at 21:56