MTSkull Posted June 10, 2004 Posted June 10, 2004 I need to expand on the info in this thread with a bit of twist. If a previous instance of the application is found I want to ask the user if they want to kill it (the previous instance and not the current.) I will end the current if they choose no, but how do I Kill the previous. Don't need complete code just pseudo code will be fine, and hint of where to start. Public Function PrevInstance() As Boolean Dim ReturnValue as Boolean = False If UBound(Diagnostics.Process.GetProcessesByName( _ Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then Dim Message As String Message = "A previous instance of this program is currently running." Message += " Do you want to end the other program?" If MsgBox(Message, MsgBoxStyle.YesNo, "Kill Prev App") _ = MsgBoxResult.Yes Then 'Kill Prev app or apps here Else ReturnValue = True End If End If Return ReturnValue End Function MTS Quote "Beer is proof that God loves us and wants us to be happy." -Benjamin Franklin
Administrators PlausiblyDamp Posted June 10, 2004 Administrators Posted June 10, 2004 (edited) Something like Public Function PrevInstance() As Boolean Dim ReturnValue As Boolean Dim apps() As System.Diagnostics.Process apps = Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName) If apps.Length > 1 Then Dim Message As String Message = "A previous instance of this program is currently running." Message &= " Do you want to end the other program?" If MessageBox.Show(Message, "Kill Prev App", MessageBoxButtons.YesNo) = DialogResult.Yes Then For Each p As Process In apps If p.Id <> Process.GetCurrentProcess.Id Then p.Kill() end if Next Else Return True End If End If Return False End Function should do the trick - although be aware the .Kill method will terminate the application without giving the user the chance to save any work etc. Edited June 10, 2004 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Administrators PlausiblyDamp Posted June 10, 2004 Administrators Posted June 10, 2004 Here's a safer version as it requests the other applications to close - this way any cleanup code (form_close etc) code will be executed. Public Function PrevInstance() As Boolean Dim ReturnValue As Boolean Dim apps() As System.Diagnostics.Process apps = Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName) If apps.Length > 1 Then Dim Message As String Message = "A previous instance of this program is currently running." Message &= " Do you want to end the other program?" If MessageBox.Show(Message, "Kill Prev App", MessageBoxButtons.YesNo) = DialogResult.Yes Then For Each p As Process In apps If p.Id <> Process.GetCurrentProcess.Id Then SendMessage(p.MainWindowHandle(), WM_CLOSE, UIntPtr.Zero, IntPtr.Zero) End If Next Else Return True End If End If Return False End Function Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
MTSkull Posted June 10, 2004 Author Posted June 10, 2004 Made a few tweaks. Since I am not worried about saving data in the other programs I am just going to kill them outright. Otherwise I would opt for the safer route. Public Function PrevInstance() As Boolean Dim ReturnValue As Boolean = False Dim apps() As System.Diagnostics.Process Dim CurrentProcess As System.Diagnostics.Process CurrentProcess = Diagnostics.Process.GetCurrentProcess apps = Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName) If apps.Length > 1 Then Dim Message As String Message = "A previous instance of this program is currently running." Message &= " Do you want to end the other program?" If MessageBox.Show(Message, "Kill Prev App", MessageBoxButtons.YesNo) = DialogResult.Yes Then For Each p As Process In apps If p.Id <> CurrentProcess.Id Then p.Kill() Next Else ReturnValue = True End If End If Return ReturnValue End Function Thanks for the help MTS Quote "Beer is proof that God loves us and wants us to be happy." -Benjamin Franklin
MTSkull Posted June 10, 2004 Author Posted June 10, 2004 Just looked up the "Return" keyword in F1. I understand now, learn something new every day. Thanks Again MTS Quote "Beer is proof that God loves us and wants us to be happy." -Benjamin Franklin
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.