每当我调用以下的构造函数Structure
(参数设置为 True
)时,我都会得到一个 NullReferenceException:
Imports System.Threading
Imports System.Windows.Threading
Public Structure Test
Private MyDispatcher As Dispatcher
Private MyResetEvent As ManualResetEvent
Public Sub New(ByVal newThread As Boolean)
If newThread Then
MyResetEvent = New ManualResetEvent(False)
Dim thread As New Thread(AddressOf Start)
thread.Start()
MyResetEvent .WaitOne()
' NullReferenceException below:
MyDispatcher.BeginInvoke(New Action(AddressOf DoSomething))
End If
End Sub
Private Sub Start()
MyDispatcher = Dispatcher.CurrentDispatcher
MyResetEvent.Set()
Dispatcher.Run()
End Sub
Private Sub DoSomething()
End Sub
End Structure
MyDispatcher
is Nothing
,这会导致 NullReferenceException。但是使用 aClass
而不是 aStructure
就可以了。为什么?
编辑:什么解决方法是可能的?