Visual Basic Development Bookmark and Share   
 Home > Visual Basic IDE > Using a Windows Service to run an other Application
 

Using a Windows Service to run an other Application

Hi,

I have a windows application that updates somedata every 10 mins. I need to make sure that this application keeps running.
I was thinking of having a service with a timer that checks if the application is running and if not it fires it up.

Can you help me out with this.
Alex Galea  Wednesday, October 28, 2009 2:10 PM


Why your application may not be running, ?

In case somebody close it ?

You can make an application impossible to close

Simply like that:


Public Class Form1

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True ' This make the application impossible to close
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Your code
    End Sub

End Class
Crazypennie  Wednesday, October 28, 2009 2:34 PM
That's a good suggestion. i will include it in my code. Thanks

But I had in mind of using a service because the application will be running on a server. So i needed that even if a user is not logged on the machine...the application would be started off by the service automatically....especially when a restart to the server would have been done remotely or so....
Alex Galea  Wednesday, October 28, 2009 3:04 PM

Thank you Crazypennie for your friendly help and good suggestion.

Hi Alex,

Welcometo MSDN forums!

Here is code sample: How to create a Windows Service and check if specified application has been run via Timer object in VB.NET.


Public Class Service1

Private Timer1 As System.Timers.Timer

Protected Overrides Sub OnStart(ByVal args() As String)

Timer1 = New System.Timers.Timer()

AddHandler Timer1.Elapsed, AddressOf Timer_Elapsed

Timer1.Interval = 1000

Timer1.Start()

End Sub

Protected Overrides Sub OnStop()

Timer1.Stop()

End Sub

Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)

'Check if your application has been run.

Dim proeslist As Process() = Process.GetProcessesByName("ApplicationName")

If proeslist.Count = 0 Then

Process.Start("D:\AppFolder\AppName.exe") ' Here start your application

End If

' Additionally, you can check each process'title and name like this:

Dim pro As Process

For Each pro In proeslist

Dim winTitle As String = pro.MainWindowTitle

Dim pName As String = pro.ProcessName

Next

End Sub

End Class


Best regards,
Martin Xie


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Martin Xie - MSFT  Tuesday, November 03, 2009 12:30 PM
Hi Martin,

Thanks for your reply.

I managed to put the service at work. In fact the application is started but after a while it closes itself down again.

Am i missing something. When i run the application standalone it works fine.

I am also getting an errorevent in the application when the application is shutdown on it's own.

Event Type:Error
Event Source:.NET Runtime 2.0 Error Reporting
Event Category:None
Event ID:5000
Date:03/11/2009
Time:16:49:16
User:N/A
Computer:OMEGA
Description:
EventType clr20r3, P1 kreaschedule.exe, P2 1.0.54.0, P3 4af04f0a, P4 kreaschedule, P5 1.0.54.0, P6 4af04f0a, P7 e, P8 e9, P9 system.invalidoperationexception, P10 NIL.
Alex Galea  Tuesday, November 03, 2009 4:05 PM

You can use google to search for other answers

Custom Search

More Threads

• How to start with Barcode scanning?
• Test projects EFT.Tests.vbproj
• VolumeControl.ocx error
• How to compile VB6 code
• Save my ____ for college will give xbox live points or w/e
• Cursor flickers and IDE Not responding ....
• Crash in VS 2005
• What happened to the bolding of brackets in the code window?
• F5 Debug for Windows Service in VS 2005
• Application Icon