Home

Threads in LinqPad

I am using LinqPad as a learning tool to enhance my C# skills, looking at the Programming in C# Exam Ref book it has the following code:

public static class Program { public static void ThreadMethod() { for (int i = 0; i < 10; i++) { Console.WriteLine("ThreadProc: {0}", i); Thread.Sleep(1000); } } public static void Main() { Thread t = new Thread(new ThreadStart(ThreadMethod)); t.IsBackground = true; t.Start(); } }

The book states that this should exit straight away as the thread is set to a IsBackground, however in LinqPad this writes out the ThreadProc 10 times. In Visual Studio this exits straight away as expected. Am I missing something in the way LinqPad handles threads.

Many Thanks

J.

Comments

  • LINQPad keeps the process alive after your code finishes running (and it re-uses the process when you re-run the query). This is a performance optimization, and it allows you to further interact with the process after the query finishes.

    Features that rely on this include hyperlinks that you can click to fetch more data (Util.OnDemand, Lazy, LINQ-to-SQL association properties, etc.), and the .Cache() extension method for sequences (or Util.Cache), which gives you the benefits of a REPL.
  • Thank you for that bit of info
Sign In or Register to comment.