Loading Notepad as a Process

Hi,

The code below is for a service that fires every 10 seconds. If it doesn't find an instance of Notepad running, it loads it up. Please note that Notepad is only for a test, the final version of the software will run a program once at 22:00.

The problem I have is that Notepad isn't loading fully. I get a Notepad.exe in the task-manager, but despite setting the properties, the actual Notepad application doesn't load for user-interaction!

Can someone explain to me what I'm doing wrong please

TIA

Imports System.ServiceProcess

Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Private _ProcessRunning As Boolean = False

#Region " Component Designer generated code "

Public Sub New()
MyBase.New()

' This call is required by the Component Designer.
InitializeComponent()

' 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 Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'


ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)

End Sub

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

' 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.
Friend WithEvents Timer1 As System.Timers.Timer
Friend WithEvents EventLog1 As System.Diagnostics.EventLog
Friend WithEvents Process1 As System.Diagnostics.Process
Friend WithEvents ServiceController1 As System.ServiceProcess.ServiceController

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Timer1 = New System.Timers.Timer
Me.EventLog1 = New System.Diagnostics.EventLog
Me.Process1 = New System.Diagnostics.Process
Me.ServiceController1 = New System.ServiceProcess.ServiceController
CType(Me.Timer1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).BeginInit()
'
'Timer1
'
Me.Timer1.Interval = 10000
'
'Process1
'
Me.Process1.StartInfo.FileName = "C:\WINDOWS\NOTEPAD.EXE"
'
'ServiceController1
'
Me.ServiceController1.ServiceName = "Watson Batch Importer"
'
'Service1
'
Me.ServiceName = "Watson Batch Import"
CType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).EndInit()

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.
Timer1.Enabled = True
End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
If Process1.HasExited = False Then
Process1.Kill()
End If
Timer1.Enabled = False
End Sub

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
Timer1.Enabled = False
Dim _StartTime As New TimeSpan(9, 0, 0)
Dim _EndTime As New TimeSpan(18, 1, 0)
Dim _Start As Integer = Now.TimeOfDay.CompareTo(_StartTime)
Dim _End As Integer = Now.TimeOfDay.CompareTo(_EndTime)

If _Start = 1 And _End = -1 And _ProcessRunning = False Then
EventLog1.WriteEntry("Watson Batch Import", "Batch entry started at " & Now.ToLongTimeString & " on " & Now.ToLongDateString)
Process1.Start()
_ProcessRunning = True
Else
If Process1.HasExited = True Then _ProcessRunning = False
End If

Timer1.Enabled = True
End Sub


End Class




Answer this question

Loading Notepad as a Process

  • SoulSolutions


    Just to verify, is it running under LocalSystem

  • Svladim

    Ahh, sorry, yes the application I'll be launching has a full user interface.


  • jollyguy


    I was referring to the user interface of the application you will be launching from the Windows service and not the service itself.

    I'm afraid I don't know anything about Sage MMS so I still don't know whether this application has a user interface.



  • Tom Guinther


    Does the application the service is launching have a user interface like Notepad If not, maybe you should be testing with a simple Visual Basic app that executes a few lines of code (w/o a user interface) and then terminates.

  • Chieng Sisovin

    To be exact, the service that is running has no interface at all. There's a simple timer on there that checks every 10 seconds to see if the system time falls between a hardcoded range, and if it does, it then (or should!) load an external application.

    The External Application in my example is Notepad, but the final version will actually be Sage MMS.




  • Craigomatic


    The following might be relevant as well since it allows you to specify credentials for the process:

    Class that wraps CreateProcessWithLogonW

    Otherwise, give the security ramifications of this implementation I would recommend creating an autorun app/stub that launches your app, instead of a Windows service.



  • Pastor Rod

    Hi David,

    Many thanks for that, I appreciate the help.

    Didn't realise what you've mentioned, it makes sense I guess.

    However I've now done what you've mentioned but still no joy.

    Since I posted the original, I've done a fair bit of searching on the net, downloaded examples and I just can't see what I'm doing differently.




  • TheIlliterate

    The 'Allow service to interact with desktop' (Service Properties -> Log On) option needs to be checked for your service. However, be aware that this option could be removed in future versions of Windows as it represents a security risk.

    For move information, see: http://msdn.microsoft.com/library/default.asp url=/library/en-us/dncode/html/secure08192002.asp.



  • Loading Notepad as a Process