Home
Options

Seems like incorrect behavior of LINQPad

I have a licensed version of Premium LINQPad 4.59.00.

I am seeing some apparently incorrect behavior with the following C# program listing. The method called ThreadMethod is being run on a background thread from the Main method and is being run always to completion.

However, when I run the same code as a console app in Visual Studio 2022 then the ThreadMethod is not being run at all. The Main method is supposed to be a foreground thread as it is in a console app run from Visual Studio 2022 and therefore when control during execution reaches the end of Main method, the console app shuts down. This expected behavior should be duplicated in LINQPad, but its not, probably because the Main method in LINQPad is a background thread unlike a console app.

void Main()
{
    Console.WriteLine("The Main method thread is background thread : {0}", Thread.CurrentThread.IsBackground);
    Thread t  = new Thread(new ThreadStart(ThreadMethod));
    t.IsBackground = true;
    t.Start();
}

// Define other methods and classes here
public void ThreadMethod()
{
    for(int i=0; i<10; i++)
    {
        Console.WriteLine("ThreadProc: {0}", i);
        Thread.Sleep(1000);

    }
}

Comments

  • Options

    LINQPad's query processes don't shut down after the main thread finishes. This makes it faster, as well as more useful as a scripting tool - features such as Util.Cache rely on preserving the process between executions and give LINQPad the benefits of a REPL environment.

    If you want to mimic the behavior in Visual Studio application, add Environment.Exit(0); to the end of your main method.

Sign In or Register to comment.