Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

  • Administrators
Posted (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 by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

Posted

Just looked up the "Return" keyword in F1. I understand now, learn something new every day.

 

Thanks Again

MTS

"Beer is proof that God loves us and wants us to be happy."

-Benjamin Franklin

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...