Hi All,
I am using a COM object and so I have to use a form for it to work.
The problem is I need to run it as a service as well.
When I run it in a test scenario everything works fine and Invoke
works as expected.
However, when I run it as a service the Invoke command just bombs out
without any errors and everything just stops. I have the service
installed using an administrator account and interacting with the
desktop for testing.
The code I use is below. 'Test' is the form with the COM object on it.
This is my test code and everything works as expected if I set
RunAsService to False and run it from the IDE.
If I compile and install as a service and set RunAsService to TRUE,
Start the service from the MMC and then
attach to it. When I get to the Me.Invoke(SP) line it just hangs until
I terminate the process. No errors or anything.
Can anyone tell me why this might be please.
Many thanks,
Mike
Imports System.ServiceP rocess
Imports System
Imports System.Diagnost ics
Imports System.Componen tModel
Public Class svcTest
Inherits System.ServiceP rocess.ServiceB ase
Private Delegate Sub InitDelegate(By Val args() As String)
Private Test As New Test
#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeCompo nent()
' Add any initialization after the InitializeCompo nent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim RunAsService As Boolean = False
If RunAsService Then
Dim ServicesToRun() As System.ServiceP rocess.ServiceB ase
ServicesToRun = New System.ServiceP rocess.ServiceB ase()
{New svcTest}
System.ServiceP rocess.ServiceB ase.Run(Service sToRun)
Else
Dim DebugService As New svcTest
Dim InitDelegate As InitDelegate = New
InitDelegate(Ad dressOf DebugService.On Start)
InitDelegate.Be ginInvoke(Nothi ng, Nothing, Nothing)
System.Windows. Forms.Applicati on.Run()
End If
End Sub
'Required by the Component Designer
Private components As System.Componen tModel.IContain er
' NOTE: The following procedure is required by the Component
Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
'
'OmegaIPVAD
'
Me.ServiceName = "OmegaIPVAD "
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set
things
' in motion so your service can do its work.
Go()
End Sub
Protected Overrides Sub OnStop()
End Sub
Sub Go()
Test.Init()
End Sub
End Class
Imports System.Threadin g
Public Class Test
Inherits System.Windows. Forms.Form
#Region "Variables"
Private Delegate Sub InvokeDelegateN oParams()
#End Region
Public Sub New()
'Even though this is a windowless form we still need to show
the form
'so that the message loop starts.
MyBase.New()
Me.Show()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
End Sub
Private Sub InitializeCompo nent()
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(168, 144)
Me.Name = "Test"
Me.ShowInTaskba r = False
End Sub
Public Sub Init()
If Me.InvokeRequir ed = True Then
Dim SP As InvokeDelegateN oParams = New
InvokeDelegateN oParams(Address Of Init)
Try
Me.Invoke(SP)
Exit Sub
Catch ex As Exception
End Try
End If
DoStuff()
End Sub
End Class