Watching a process.

I'm having trouble with this. Basically what I'm trying to accomplish is a server restarter. It needs to look for the server to close. I was thinking just look for the process to close. But I'm not sure how to do it. Could someone help please

Answer this question

Watching a process.

  • John Hennesey

    If you write the server code you could also add a named Mutex (see MSDN Mutex class), which your server creates. When the server crashes the mutex will also die. Check the existence of that mutex in your watcher. If you come up with a really unique mutex name, that collides not as easily as process names might do.

    --
    SvenC


  • Corngood

    Thanks for this info. I'm still kind of confused as I am just learning VB, but your help is very much appreciated. If theres more specific info you could give I would be much obliged. If not thats quite alright you've helped quite a bit already. I will mark this post as answered but if you want I wouldn't mind more information if possible.
  • uaurgent

    GetProcessesByName returns an array of Process objects, because there can be more with the same name. Loop that array with

    for each oProc As Process in Process.GetProcessesByName("yourApp")

    ' see oProc.MainModule.ModuleName or .FileName for your process name and or path

    next

    You could use the path if you now where your app is installed to see if it is really your app.

    --
    SvenC


  • Michael Herman - Parallelspace

    Do you use the term "server" for your hardware or your software If software: is your server implementes as Windows Service If yes: in the Services console open the properties of your service and configure "Recovery" to restart your service after if failed. Basically Windows gives you this process watchdog feature for free.

    --
    SvenC


  • Mateusz Rajca

    Ok, but does GetProcessByName return a value like 1 if it exists
  • Alexandre Defalque

    For anyone who may be looking for a solution to this here is the code I used. It may not be the best way to do it. But it works


    Public Class Form1

    Public proc1 As String

    Dim proc As New System.Diagnostics.Process()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    timer.Interval = Integer.Parse(intBox.Text)

    timer.Enabled = True

    start()

    End Sub

    Private Sub start()

    proc1 = procBox.Text

    ' Fill-up StartInfo

    proc.StartInfo.UseShellExecute = True

    proc.StartInfo.FileName = proc1

    ' Start the process

    proc.Start()

    End Sub

    Private Sub timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer.Tick

    If (proc.HasExited) Then

    start()

    End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    timer.Enabled = False

    End Sub

    End Class


  • venp

    OK, in your watching app use System.Diagnostics.Process.GetProcessesByName to check if your app is returned in that collection. Do that check on a regular basis e.g. with a Timer component.

    --
    SvenC


  • Chrisull

    Sorry I should have been more specific. Server is refering to software and no it's not registered as a service.

    I could have it registered as one. But I would much rather understand how to do it this way.


  • rolian

    I do have access to the servers code, it's in C++ though. Not exactly a pro in that either and I havent heard of the idea of mutex before.
  • Watching a process.