Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Environment.Exit cause LINQPad to terminate

Tags:

c#

linqpad

I am debugging a program containing Environment.Exit(0); in LINQPad.

When this line Environment.Exit(0); hits, LINQPad exits.

  1. Why does it happen? (In Visual Studio, obviously just the process terminates).
  2. Is there a way to prevent this behavior without changing the original source ?

Simple example:

void Main()
{
    Environment.Exit(0);
}
like image 799
Ofiris Avatar asked Dec 14 '22 19:12

Ofiris


2 Answers

This happens because LINQPad executes queries in the same process as itself, isolating queries via application domain rather than process.

This has changed in the latest beta: LINQPad now runs each query in its own process. This is in preparation for the upcoming integrated debugger (it's impossible debug your own process). A pleasant side-effect is that calling Environment.Exit (or throwing a StackOverflowException) no longer crashes the host process.

like image 137
Joe Albahari Avatar answered Jan 01 '23 07:01

Joe Albahari


LINQPad hosts executed code in an asynchronous thread (to not lock the UI) of its own process, so when you run instructions such as Environment.Exit, hosting process (hence Linqpad itself) is affected too.

You can easily check this by running a long program which makes some calculations : there is no new process in task manager, and linqpad process CPU usage is raising according to computations.

You can tweak a few things in Advanced Options related to application domain for example, but AFAIK there is no way to avoid this behavior since Linqpad entirely relies on this way to execute code.

like image 23
AFract Avatar answered Jan 01 '23 08:01

AFract