title: New DataContext Just Crashes; With No Exceptions
body: <p>I am creating a Windows Service in VB.NET that will loop over a set of functions indefinitely, so threading is necessary.  You can't just run your loop in <code>OnStart()</code>; it has to return to the Service Controller.</p>
  
  <p>My program completely crashes after <code>New DataContext</code> with <strong>no exceptions</strong> or anything.</p>
  
  <p>I have removed a lot of code that <em>seems</em> irrelevant to me, so that you can see the main problem code better.  I have removed all <code>Try</code> blocks and changed my <code>Meter</code> types to <code>Object</code> types, so you can copy and paste mostly working code.</p>
  
  <p>I know this is a problem with <a href="http://weblog.west-wind.com/posts/2008/Feb/05/Linq-to-SQL-DataContext-Lifetime-Management" rel="nofollow noreferrer">DataContext and threading</a> that I don't understand.  <a href="https://stackoverflow.com/questions/226127/multiple-single-instance-of-linq-to-sql-datacontext?lq=1">Here's a summary of MultiThreading and DataContext concerns.</a>  What specifically am I doing wrong?  </p>
  
  <pre><code>Public Class Molly
      Dim stopping As Boolean = False
      Dim stoppedEvent As Threading.ManualResetEvent
  
      Protected Overrides Sub OnStart(ByVal args() As String)
  
          'vv~~~~ If these two are uncommented, my code fails at an indeterminate place AFTER the New ConfigInterpreter below.
          'Dim Example1 As New LocalDBDataContext
          'Dim Example2 As New ConfigInterpreter
  
          Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(AddressOf ThreadWorker))
      End Sub
  
      Protected Overrides Sub OnStop()
          stopping = True
          stoppedEvent.WaitOne()
      End Sub
  
      Private Sub ThreadWorker()
          Dim MyCI As New ConfigInterpreter '&lt;~~~~ 'My program fails here every time, 
                                                   'unless the two lines above are uncommented,
                                                   'then it fails at a random point after this.
  
          Dim ListOfMeters as List(Of Meter) = MyCI.CompileListOfMeters()
  
          While stopping = False
                  For Each mtr In ListOfMeters()
                      mtr.Read()
                  Next
  
                  Threading.Thread.Sleep(My.Settings.CheckIntervalMS)
          End While
  
          stoppedEvent.Set()
      End Sub
  End Class 'Molly
  
  Public Class ConfigInterpreter
      Public Sub New()
          Try
  
              Dim LocalDB As New LocalDBDataContext '&lt;~~~~~~~ My program typically crashes here.
  
          Catch ex As Exception '&lt;~~~~ No exception is thrown.
  
          End Try
      End Sub
  
      Public Function CompileListOfMeters() As List(Of Object)
          Dim LocalDB As New LocalDBDataContext
          Dim qMeters As IQueryable(Of tblMeter) = From ff In LocalDB.tblMeters
                                               Select ff
  
          Dim ReturnList As New List(Of Object) 
          For Each mtr In qMeters
              ReturnList.Add(mtr)
          Next
  
          Return ReturnList
      End Function
  End Class 'ConfigInterpreter
  </code></pre>