473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Single Application Run

How do I make it so that once my application (.exe) is running, if the user
tries to run a second instance of the program, it is not allowed.

I want my program to be able to have only one instance of it running,
period.

Thx,

-Tom

Jul 13 '06 #1
3 8820
Tom Rogers wrote:
How do I make it so that once my application (.exe) is running, if
the user tries to run a second instance of the program, it is not
allowed.

I want my program to be able to have only one instance of it running,
period.
There are quite a number of ways. The simplest is something like this:

Sub Main
If App.PrevInstance Then Exit Sub
...

If you know the caption of your app, you can enhance this like:

Sub Main
If App.PrevInstance Then
AppActivate MyCaption
Exit Sub
End If
...

For more ideas, see http://vb.mvps.org/samples/PrevInst
--
Jul 13 '06 #2
Google is a good friend :)
From VBForums-ClassicVB by dee-u.

Method 1: You can read the PrevInstance property of the App object, if the
value of this is True then another instance of the application is already
running.

If your program starts with Sub Main, you can use this code to exit the
program if another copy is already running:
visual basic
code:-----------------------------------------------------------------------
-------'in sub main...
If App.PrevInstance = True Then
MsgBox "Already running...."
Exit Sub
End If
----------------------------------------------------------------------------
--For forms you need an extra piece of code to also close the form, the
following code should be placed in Form_load:
visual basic
code:-----------------------------------------------------------------------
-------'in form_load...
If App.PrevInstance = True Then
MsgBox "Already running...."
Unload Me
Exit Sub
End If
----------------------------------------------------------------------------
--

Method 2: You can use the FindWindow API to search for open windows with
your form caption.

Note that for this to work you must know the exact title to find, so if you
change your window title within your program

(such as adding a file name, like Notepad does) then this will fail. Also,
if other running programs have the same caption,

your program may not run at all.

visual basic
code:-----------------------------------------------------------------------
-------
Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Long

Public Sub Main()
Dim m_hWnd As Long

'Change Form1 to your form Caption.
m_hWnd = FindWindow(vbNullString, "Form1")
If m_hWnd 0 Then
MsgBox "Program " & App.Title & " is already running..."
Exit Sub
End If

'Change Form1 to your form name.
Form1.Show
End Sub
/Henning"Tom Rogers" <jeditom@NO_SPAMtwcny.rr.comskrev i meddelandet
news:6N******************@twister.nyroc.rr.com...
How do I make it so that once my application (.exe) is running, if the
user
tries to run a second instance of the program, it is not allowed.

I want my program to be able to have only one instance of it running,
period.

Thx,

-Tom

Jul 14 '06 #3
Untested
http://www.planetsourcecode.com/vb/s...65732&lngWId=1

/Henning

"Tom Rogers" <jeditom@NO_SPAMtwcny.rr.comskrev i meddelandet
news:6N******************@twister.nyroc.rr.com...
How do I make it so that once my application (.exe) is running, if the
user
tries to run a second instance of the program, it is not allowed.

I want my program to be able to have only one instance of it running,
period.

Thx,

-Tom

Jul 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: BPearson | last post by:
Hello I would like to have several sites share a single web.config file. To accomplish this, I would point the root of these sites to the same folder. Is there any reason why I might not want to...
3
by: Carl Johansen | last post by:
I have a big ASP website (used by several thousand car dealers) that is a collection of lots of small and medium-sized applications. Now I want to start adding ASP.NET applications to it. I have...
9
by: MrSpock | last post by:
1. Create a new Windows Application project. 2. Open the project properties and check "Make single instance application". 3. Build. 4. Go to the release folder and run the application. 5. Try to...
3
by: Joe Fawcett | last post by:
Dear All We have an application where users login, their name/password details are stored in a table, and now we want to add a third party bulletin board that lives in its own virtual directory....
11
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a...
7
by: ashley.ward | last post by:
We have been using VB6 to develop small custom apps that access an Oracle database, in order to extend a larger product that is developed by our colleagues in Germany (who use C++ and Java). As...
6
by: kumar_anil_gaya_India | last post by:
Hi All, I am facing one problem. I have one DLL called LogManagement which has ability to write to ExceptionLog and EventLog File. Both are separate file locate in Logs Diectory. I have two ...
1
by: xtseeker | last post by:
We have a multi-threaded .NET application, the problem is that the previous programmer didn't care to have locks on the shared variables. Somehow the application doesn't end up in Locked state on a...
3
by: google | last post by:
Hi I'm looking for some advice, so if anyone wants to contribute I would be grateful. I'm trying to create a web application. By application I mean something that is like a Windows...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.