Tvde1
Tvde1

Reputation: 1284

Will Environment.Exit(int) kill my application with treads running unmanaged code?

I have a windows service which runs unmanaged code (using DllImport) in different threads.

Sometimes, the unmanaged code 'hangs'. Think of while (true) ;. When that happens, I need to kill the entire process (which automatically starts another because it's a windows service).

Is Environment.Exit(int) sufficient? Or will I need e.g. Environment.FailFast(string)?

Edit: I am unable to 'test' this. The freezes happen randomly.

Upvotes: 1

Views: 806

Answers (2)

Robert McKee
Robert McKee

Reputation: 21477

Yes. Environment.Exit will kill all the threads running in the current process, including the main thread (and the process itself).

Environment.FailFast will log an event into the Application Log and then kill the process and all of the threads in the current process.

Upvotes: 1

Martin
Martin

Reputation: 16433

From the official Microsoft documentation, Environment.Exit:

Terminates this process and returns an exit code to the operating system.

More usefully, the documentation goes on to state:

  • Exit always terminates an application
  • Exit terminates an application immediately, even if other threads are running

It sounds like Environment.Exit is perfectly sufficient for your needs.

Upvotes: 1

Related Questions