[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

How do i add persistence

Hello.
I have a question. I want to add to my application persistence.
How do i make if the application is closed to start again?
Currently i am using this code:
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length < 2 Then
          Process.Start(Process.GetCurrentProcess.ProcessName) 
     End If

Open in new window


The problem is.. under form is workign ok, but if i am using it under sub main then the process starts again and again and again leading to computer crash.
Any help would be great.
Best regards.
0
noss2011
Asked:
noss2011
  • 10
  • 4
1 Solution
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hello, you need to create a second application (a watch dog), that do the monitoring of your other application, if it is close the watch dog can know it and start it again.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
The watch dog can be the same application started with other parameter, so it can be waching each other.
0
 
noss2011Author Commented:
I understand, but i need a code or something tostart from, if anyone can help me it would be great.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Carlos VillegasFull Stack .NET DeveloperCommented:
Im on it ;)
0
 
noss2011Author Commented:
Thank you. You have a beer from me if you help me with this.

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Ok, create a new Windows forms project, in your main form add a timer, set enabled=true and interval to 2000 (2 seconds for testing), then paste this code in your form, run it:

Public Class Form1
    Dim myProcessName As String = System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath)
    Dim myProcessId As Integer = System.Diagnostics.Process.GetCurrentProcess().Id

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim starOver As Boolean = True

        For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcessesByName(myProcessName)
            If Not p.Id.Equals(myProcessId) Then
                starOver = False
            End If
        Next

        If starOver Then
            System.Diagnostics.Process.Start(Application.ExecutablePath)
        End If
    End Sub

End Class

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Important, build and run the exe manually, debugging may not work well... I think...
0
 
noss2011Author Commented:
I modified myProcessName to get the path to my application and is not working.
This is the full code.

Public Class Form1
    Dim myProcessName As String = Environment.GetEnvironmentVariable("windir") & "\temp\WatchDog.exe"
    Dim myProcessId As Integer = System.Diagnostics.Process.GetCurrentProcess().Id


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim starOver As Boolean = True

        For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcessesByName(myProcessName)
            If Not p.Id.Equals(myProcessId) Then
                starOver = False
            End If
        Next

        If starOver Then
            System.Diagnostics.Process.Start(Application.ExecutablePath)
        End If
    End Sub
End Class

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Well, that is wrong, I dont understand what you want to do... and dont work because myProcessName is not a full path, it must be only the file name without extension, use:
Dim myProcessName As String = System.IO.Path.GetFileNameWithoutExtension(Environment.GetEnvironmentVariable("windir") & "\temp\WatchDog.exe")

Open in new window


But this line must be that full path:
System.Diagnostics.Process.Start(Environment.GetEnvironmentVariable("windir") & "\temp\WatchDog.exe")

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Or just:
Dim myProcessName As String = "WatchDog"

Open in new window

0
 
noss2011Author Commented:
Thank's, but is working only half a way.
The same as before (regarding my first post) it keeps openning the process over and over again.
I want to start only once and only if is closed to start it again.
Please send me your paypal to send you a gift for trying to help me.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Thanks buddy, I appreciate your gesture but that is not necessary :)

It must work, you can download the full project from this link:
http://dl.dropbox.com/u/13237304/WatchDogProject.zip

And the exe, just extract the exe to your desktop and try it:
http://dl.dropbox.com/u/13237304/WatchDog.zip
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
You need understand that my demo only work for it self, is not working to open your other application, to do that yo need implement the same monitoring method in both applications.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Thanks!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 10
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now