Ive been banging my head on this now for days. I am developing a com object in vb.net to display a splash screen. This will be called from vb script and Powershell to provide status to users while other instructions are occurring in the script. I am receiving this error intermittently any help would be appreciated.
Here is a link that I have been using to help to develop my application. http://msdn.microsoft.com/en-us/library/ms233639.aspx
Public Class Progress
Private WithEvents appContext As ApplicationContext
Private frmSplash As frmProgress
Private Delegate Sub FormDelegate()
Private ShowDelegate As FormDelegate
Private UnLdDelegate As FormDelegate
Private t As Thread
Public Sub New()
MyBase.New()
If IsNothing(appContext) Then
frmSplash = New frmProgress
appContext = New ApplicationContext(frmSplash)
t = New Thread(AddressOf StartMessageLoop)
t.IsBackground = True
t.SetApartmentState(ApartmentState.STA)
t.Start()
ShowDelegate = New FormDelegate(AddressOf frmSplash.Show)
HideDelegate = New FormDelegate(AddressOf frmSplash.Hide)
UnLdDelegate = New FormDelegate(AddressOf frmSplash.Close)
End If
End Sub
MessageLoop
Private Sub StartMessageLoop()
Application.Run(appContext)
End Sub
ShowForm
Public Sub Show()
If frmSplash.InvokeRequired Then
If frmSplash.IsHandleCreated = True Then
appContext.MainForm.Invoke(ShowDelegate)
End If
End If
End Sub
Unload
Public Sub Unload()
If frmSplash.InvokeRequired Then
If frmSplash.IsHandleCreated Then
appContext.MainForm.Invoke(UnLdDelegate)
End If
End If
End Sub
Thread Exit Event
Private Sub ac_ThreadExit(ByVal sender As Object, ByVal e As System.EventArgs) Handles appContext.ThreadExit
appContext.MainForm.Dispose()
appContext.MainForm = Nothing
appContext.Dispose()
appContext = Nothing
frmSplash.Dispose()
frmSplash = Nothing
End Sub
The error is generally being thrown when i call the Show Method from a powershell script. Any help would be greatly appreciated. Thanks
Complete Code
Private ShowLock As New ManualResetEvent(False)
Private WithEvents appContext As ApplicationContext
Private frmSplash As frmProgress
Private Delegate Sub FormDelegate()
Private Delegate Sub DisplayDelegate(ByVal msg As String)
Private Delegate Sub FormSettings(ByVal val As Boolean)
Private ShowDelegate As FormDelegate
Private HideDelegate As FormDelegate
Private UnLdDelegate As FormDelegate
Public Sub New()
MyBase.New()
frmSplash = New frmProgress
End Sub
Public Sub SetDisplayText1(ByVal msg As String)
If frmSplash.InvokeRequired Then
Dim d As New DisplayDelegate(AddressOf SetDisplayText1)
frmSplash.Invoke(d, New Object() {[msg]})
Else
frmSplash.Label1.Text = msg
frmSplash.Label1.Refresh()
End If
End Sub
Public Sub SetDisplayText2(ByVal msg As String)
If frmSplash.InvokeRequired Then
Dim d As New DisplayDelegate(AddressOf SetDisplayText2)
frmSplash.Invoke(d, New Object() {[msg]})
Else
frmSplash.Label2.Text = msg
frmSplash.Label2.Refresh()
End If
End Sub
Public Sub SetTitle(ByVal msg As String)
If frmSplash.InvokeRequired Then
Dim d As New DisplayDelegate(AddressOf SetTitle)
frmSplash.Invoke(d, New Object() {[msg]})
Else
frmSplash.Text = msg
End If
End Sub
Public WriteOnly Property AlwaysOnTop() As Boolean
Set(ByVal value As Boolean)
SetTop(value)
End Set
End Property
Public Sub Show()
AddHandler frmSplash.HandleCreated, AddressOf frm_HandleCreated
If IsNothing(appContext) Then
Dim t As Thread
appContext = New ApplicationContext(frmSplash)
t = New Thread(AddressOf StartMessageLoop)
t.IsBackground = True
t.SetApartmentState(ApartmentState.STA)
t.Start()
Else
ShowLock.WaitOne()
If frmSplash.InvokeRequired Then
If frmSplash.IsHandleCreated = True Then
ShowDelegate = New FormDelegate(AddressOf Show)
appContext.MainForm.Invoke(ShowDelegate)
End If
Else
frmSplash.Show()
End If
End If
End Sub
Public Sub SetTop(ByVal val As Boolean)
If frmSplash.InvokeRequired Then
Dim d As New DisplayDelegate(AddressOf SetTop)
frmSplash.Invoke(d, New Object() {[val]})
Else
frmSplash.TopMost = val
frmSplash.TopLevel = val
End If
End Sub
Public Sub Hide()
If frmSplash.InvokeRequired Then
If frmSplash.IsHandleCreated Then
HideDelegate = New FormDelegate(AddressOf Hide)
appContext.MainForm.Invoke(HideDelegate)
End If
Else
frmSplash.Hide()
End If
End Sub
Public Sub Unload()
If frmSplash.InvokeRequired Then
If frmSplash.IsHandleCreated Then
UnLdDelegate = New FormDelegate(AddressOf Unload)
appContext.MainForm.Invoke(UnLdDelegate)
End If
Else
frmSplash.Close()
End If
End Sub
Private Sub StartMessageLoop()
Application.Run(appContext)
End Sub
Private Sub ac_ThreadExit(ByVal sender As Object, ByVal e As System.EventArgs) Handles appContext.ThreadExit
appContext.MainForm.Dispose()
appContext.MainForm = Nothing
appContext.Dispose()
appContext = Nothing
frmSplash.Dispose()
frmSplash = Nothing
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Public Sub frm_HandleCreated()
ShowLock.Set()
End Sub
My Debug Lines would lead me to believe that the crash is occurring here
Public Sub Show()
AddHandler frmSplash.HandleCreated, AddressOf frm_HandleCreated
If IsNothing(appContext) Then
Dim t As Thread
appContext = New ApplicationContext(frmSplash)
t = New Thread(AddressOf StartMessageLoop)
t.IsBackground = True
t.SetApartmentState(ApartmentState.STA)
t.Start()
Thanks for all the help so far.