By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,276 Members | 2,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,276 IT Pros & Developers. It's quick & easy.

Can anyone tell me why Invoking from a service doesn't work please? - Service Problem.txt (0/1)

P: n/a
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,

Imports System.ServiceProcess
Imports System
Imports System.Diagnostics
Imports System.ComponentModel

Public Class svcTest
Inherits System.ServiceProcess.ServiceBase
Private Delegate Sub InitDelegate(ByVal args() As String)
Private Test As New Test

#Region " Component Designer generated code "

Public Sub New()

' This call is required by the Component Designer.

' Add any initialization after the InitializeComponent() call

End Sub

'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
If disposing Then
If Not (components Is Nothing) Then
End If
End If
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.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase()
{New svcTest}
System.ServiceProcess.ServiceBase.Run(ServicesToRu n)
Dim DebugService As New svcTest
Dim InitDelegate As InitDelegate = New
InitDelegate(AddressOf DebugService.OnStart)
InitDelegate.BeginInvoke(Nothing, Nothing, Nothing)
End If

End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
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
' in motion so your service can do its work.
End Sub

Protected Overrides Sub OnStop()

End Sub

Sub Go()
End Sub
End Class
Imports System.Threading

Public Class Test
Inherits System.Windows.Forms.Form

#Region "Variables"

Private Delegate Sub InvokeDelegateNoParams()

#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.

'This call is required by the Windows Form Designer.

End Sub

Private Sub InitializeComponent()

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(168, 144)
Me.Name = "Test"
Me.ShowInTaskbar = False
End Sub

Public Sub Init()
If Me.InvokeRequired = True Then
Dim SP As InvokeDelegateNoParams = New
InvokeDelegateNoParams(AddressOf Init)
Exit Sub
Catch ex As Exception
End Try
End If

End Sub
End Class
Nov 20 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.