Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by atomicgs12
at 2024-04-06 19:27:25
Point:500 Replies:15 POST_ID:828463USER_ID:11280
Topic:
Microsoft Visual Basic.Net;;Visual Basic Programming
I would like some help, maybe some code example, of how to stop a Visual Basic application from starting if another instance of the same app is already running. I know how to do this in C/C++ but I'm really not a VB guy.
Thanks
Thanks
Expert: mlmcc replied at 2024-07-24 12:03:59
I always use this following code below and work fine
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const WM_CLOSE = &H10
Dim hdlg as long
Dim b as variant
hdlg=0
hdlg=Findwindow(vbNullstring, " Microsoft VIsual Basic---your VBA program name")
b=Time()
Do until hdlg>0 or TimeValue(Time()) - TimeValue(b) > TimeValue("00:00:10"), 'after 10 second, no more looping
if hdlg>0 then
SendMessageSTRING hdlg, WM_CLOSE, 0, 0 ' close all VBA program at specified program name
loop
'Open new VBA program here
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const WM_CLOSE = &H10
Dim hdlg as long
Dim b as variant
hdlg=0
hdlg=Findwindow(vbNullstring, " Microsoft VIsual Basic---your VBA program name")
b=Time()
Do until hdlg>0 or TimeValue(Time()) - TimeValue(b) > TimeValue("00:00:10"), 'after 10 second, no more looping
if hdlg>0 then
SendMessageSTRING hdlg, WM_CLOSE, 0, 0 ' close all VBA program at specified program name
loop
'Open new VBA program here
Expert: mlmcc replied at 2024-07-24 12:03:58
I always use this following code below and work fine
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const WM_CLOSE = &H10
Dim hdlg as long
Dim b as variant
hdlg=0
hdlg=Findwindow(vbNullstring, " Microsoft VIsual Basic---your VBA program name")
b=Time()
Do until hdlg>0 or TimeValue(Time()) - TimeValue(b) > TimeValue("00:00:10"), 'after 10 second, no more looping
if hdlg>0 then
SendMessageSTRING hdlg, WM_CLOSE, 0, 0 ' close all VBA program at specified program name
loop
'Open new VBA program here
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const WM_CLOSE = &H10
Dim hdlg as long
Dim b as variant
hdlg=0
hdlg=Findwindow(vbNullstring, " Microsoft VIsual Basic---your VBA program name")
b=Time()
Do until hdlg>0 or TimeValue(Time()) - TimeValue(b) > TimeValue("00:00:10"), 'after 10 second, no more looping
if hdlg>0 then
SendMessageSTRING hdlg, WM_CLOSE, 0, 0 ' close all VBA program at specified program name
loop
'Open new VBA program here
Author: atomicgs12 replied at 2024-05-20 16:09:03
Still haven't been able to apply any of the suggestions to see which one works. I'll post as soon as I get back to this project. Thanks to all
Assisted Solution
Expert: duncanb7 replied at 2024-05-05 12:08:07
166 points EXCELLENT
I always use this following code below and work fine
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const WM_CLOSE = &H10
Dim hdlg as long
Dim b as variant
hdlg=0
hdlg=Findwindow(vbNullstring, " Microsoft VIsual Basic---your VBA program name")
b=Time()
Do until hdlg>0 or TimeValue(Time()) - TimeValue(b) > TimeValue("00:00:10"), 'after 10 second, no more looping
if hdlg>0 then
SendMessageSTRING hdlg, WM_CLOSE, 0, 0 ' close all VBA program at specified program name
loop
'Open new VBA program here
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const WM_CLOSE = &H10
Dim hdlg as long
Dim b as variant
hdlg=0
hdlg=Findwindow(vbNullstring, " Microsoft VIsual Basic---your VBA program name")
b=Time()
Do until hdlg>0 or TimeValue(Time()) - TimeValue(b) > TimeValue("00:00:10"), 'after 10 second, no more looping
if hdlg>0 then
SendMessageSTRING hdlg, WM_CLOSE, 0, 0 ' close all VBA program at specified program name
loop
'Open new VBA program here
Expert: Russell_Venable replied at 2024-04-21 12:35:17
This should do it for you. Glad to see your back.
Author: atomicgs12 replied at 2024-04-21 12:10:09
Sorry haven't been able to get back to this project to try the last suggestion.
Accepted Solution
Expert: Russell_Venable replied at 2024-04-07 04:47:39
168 points EXCELLENT
That's not a good way. Ther are apt of instances where that would go wrong. Try using this approach instead.
Using Mutex
Using Mutex
//.NET implementation as you have to write a whole class and import functions to make this kind of call using CreateMutex from kernel32.dll import.Using yourmutex As Mutex = New Mutex(True, "MyRunningApp", instanceCountOne)If instanceCountOne Then//run program Else MessageBox.Show("An application instance is already running") 1:2:3:4:5:6:7:
Assisted Solution
Expert: CodeCruiser replied at 2024-04-07 04:30:23
166 points EXCELLENT
Make sure you have opened the correct project properties.
Here is another technique
http://www.codeproject.com/KB/vb/sing_inistan.aspx
another one
http://www.bobpowell.net/singleinstance.htm
Here is another technique
http://www.codeproject.com/KB/vb/sing_inistan.aspx
another one
http://www.bobpowell.net/singleinstance.htm
Expert: andr_gin replied at 2024-04-07 04:11:48
You could do the following:
At application startup try to use a TcpClient and connect to a defined port.
If that fails, you are the first instance. Start a TcpListener on that port and continue your program.
If connecting works you are the second instance and can transfer any data to the first instance and close your application afterwards.
At application startup try to use a TcpClient and connect to a defined port.
If that fails, you are the first instance. Start a TcpListener on that port and continue your program.
If connecting works you are the second instance and can transfer any data to the first instance and close your application afterwards.
Author: atomicgs12 replied at 2024-04-06 22:08:55
What kind of project is it?
Windows Application
Was it upgraded from an earlier version?
Could have been but I did not do the upgrade.
Do you have an "Enable Application Framework" box?
Where, there's alot of places to look?
Windows Application
Was it upgraded from an earlier version?
Could have been but I did not do the upgrade.
Do you have an "Enable Application Framework" box?
Where, there's alot of places to look?
Expert: Mike Tomlinson replied at 2024-04-06 21:44:18
What kind of project is it?
Was it upgraded from an earlier version?
Do you have an "Enable Application Framework" box?
Was it upgraded from an earlier version?
Do you have an "Enable Application Framework" box?
Author: atomicgs12 replied at 2024-04-06 21:42:21
This is an older project I am working on. Even thought I am compiling under VS2005 going to PropertiesApplication tap there is NO 'Make single instance application' check box. Actually there are not check boxes on the Application page and no 'Windows application framework properties' text.
My solutions as multiple projects under it if I go to my solutions property under Common PropertiesStartup Project it does have 'Single startup project' check box with my startup project highlighted and this is checked. But when I run my application I can still start several different instances. I guess I need some way via code to make sure another instance does not start.
Thanks
My solutions as multiple projects under it if I go to my solutions property under Common PropertiesStartup Project it does have 'Single startup project' check box with my startup project highlighted and this is checked. But when I run my application I can still start several different instances. I guess I need some way via code to make sure another instance does not start.
Thanks
Expert: Mike Tomlinson replied at 2024-04-06 20:53:36
;)
Expert: Mike Tomlinson replied at 2024-04-06 20:53:12
What version Visual Basic are you using?...
If VB.Net 2005 (or above), just go into Project --> Propertes and CHECK the "Make single instance application" box. Done!
http://msdn.microsoft.com/en-us/library/8fz4ssw2(VS.90).aspx
If VB.Net 2005 (or above), just go into Project --> Propertes and CHECK the "Make single instance application" box. Done!
http://msdn.microsoft.com/en-us/library/8fz4ssw2(VS.90).aspx
Expert: kaufmed replied at 2024-04-06 20:51:24
In VB, there is a very easy way to do this: open the project properties, go to the Application tab, then check the "Make single instance application" checkbox.