新的DataContext崩溃了;没有例外

时间:2014-07-23 00:07:23

标签: vb.net multithreading linq windows-services datacontext

我在VB.NET中创建一个Windows服务,它将无限循环遍历一组函数,因此需要进行线程处理。你不能在OnStart()中运行你的循环;它必须返回服务控制器。

我的程序在New DataContext之后使用无异常或其他任何内容完全崩溃。

我删除了许多似乎与我无关的代码,以便您可以更好地查看主要问题代码。我删除了所有Try个版块,并将我的Meter类型更改为Object类型,因此您可以复制并粘贴大部分正常工作的代码。

我知道DataContext and threading这是一个我不明白的问题。 Here's a summary of MultiThreading and DataContext concerns.具体到底我做错了什么?

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 '<~~~~ '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 '<~~~~~~~ My program typically crashes here.

        Catch ex As Exception '<~~~~ 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

0 个答案:

没有答案