Microsoft 开发人员网络 > 论坛主页 > Visual Basic IDE > Using a Windows Service to run an other Application
提出问题提出问题
 

问题Using a Windows Service to run an other Application

  • 2009年10月28日 14:10Alex Galea 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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.

全部回复

  • 2009年10月28日 14:34Crazypennie 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     包含代码


    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
    
  • 2009年10月28日 15:04Alex Galea 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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....
  • 2009年11月3日 12:30Martin Xie - MSFTMSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Thank you Crazypennie for your friendly help and good suggestion.

    Hi Alex,

    Welcome to 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.
  • 2009年11月3日 16:05Alex Galea 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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 error event 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.