Solved

Check if application already running in VB .NET

Posted on 2004-09-28
1
175 Views
Last Modified: 2013-12-26
The App.PrevInstance function is deprecated and no longer usable in .NET.  There is no equivalent, but the same result can be obtained via:

Public Function PrevInstance() As Boolean
  If Diagnostics.Process.GetProcessesByName _
     (Diagnostics.Process.GetCurrentProcess.ProcessName) _
               .Length > 1 Then
     Return True
  Else
     Return False
  End If
End Function

My problem is that whenever I make a call to the Diagnostics.Process.xxx objects, my application stalls and needs to be manually end tasked.  For trouble shooting purposes I have tried:
msgbox(Diagnostics.Process.GetCurrentProcess.ProcessName)
This also freezes the application.  Even a completely empty application with only one form and with this msgbox line in the form load function freezes.  Any ideas?

On another note, my application is one that runs solely in the system tray as a system tray icon, with no visible windows.  The way I have accomplished this is by setting the form that loads with the project to start minimized and then doing me.hide() on form load.  Is this a good/the right way to do this?

Thanks.
0
Comment
Question by:JeffN825
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 12176534
This worked correctly for me:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If PrevInstance() Then
            MsgBox("another instance is running")
        End If
    End Sub

    Private Function PrevInstance() As Boolean
        If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

End Class
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question