Home

Cancelling a query should not clear cached results

I've been trying to develop a script to update a number of database tables from a single table. I'll be processing at least a couple million rows. Reading in each of those initial rows is a fairly expensive operation so I was counting on LINQPad on caching those rows as I work out what's working and what's not.

However, I noticed that if a query is running longer than I wanted and cancelled it to update, rerunning the script caused the queries to be reevaluated again and is not using cached results. I'm aware that I could manually clear the application domain, but I'm not trying to do that here.

// contrived example Util.Cache<string>(() => "hello world!".Dump("evaluated"), "key").Dump("cached"); Thread.SpinWait(Int32.MaxValue);

Could this behavior be changed?

Comments

  • edited March 2016
    LINQPad kills the query process when you cancel to ensure that any other threads or tasks that you may create don't continue running and pollute the next execution.

    You can prevent this behavior by enabling the option "Always preserve application domains" in Edit | Preferences | Advanced. This will force LINQPad to preserve the process/domain upon cancellation, except in the unusual case that the thread cannot be aborted.

    Bear in mind that Thread.SpinWait just happens to be one of the cases that prevents the thread from being aborted. You can fix this by replacing Thread.SpinWait with Thread.Sleep or while(true).
  • Ah ok. I think at the time, that option was set to false (the default) but I enabled it some time since then. I think I was playing around with that when looking at my example but couldn't get it to use the cached copy, I guess it was just a bad example. An equivalent of my original query with that option enabled does used cached results. I'll make sure to leave it on when I'm working on these kinds of queries in the future. Thanks for confirming.
Sign In or Register to comment.