Diese Seite mit anderen teilen ...

Informationen zum Thema:
Forum:
WinDev Forum
Beiträge im Thema:
6
Erster Beitrag:
vor 1 Monat, 2 Wochen
Letzter Beitrag:
vor 1 Monat, 1 Woche
Beteiligte Autoren:
André Labuschagné, Yogi Yang, Peter Holemans, Piet van Zanten

Running More Than One Instance

Startbeitrag von André Labuschagné am 16.03.2017 13:01

Hi All

My next problem. I only want one instance of an app running on a computer at a time. The app that is not running but is in the System Tray. If the user launches a new instance I want it to not run and not return any message. It must make the app that is already running active and then shut itself down.

How to?

Cheers
Andre

Antworten:

Hi Andre,

In the project init code:
IF ExeRunning(YourExe) THEN
EndProgram()
END

Cheers,

Peter Holemans

von Peter Holemans - am 16.03.2017 13:52
Hi André,

When creating the executable there should be an option to allow multi-instance or single-instance.

Kind regards,
Piet

von Piet van Zanten - am 16.03.2017 14:54
Peter - I saw that. Before ending the program I want to bring the app that is already running into focus. How would you do that?

I also worry about the notes in the docs where they say that this may not work where the user has insufficient rights to list the processes in memory or the application could not access the system DLL used to enumerate the processes. Under what circumstances would this be the case?

Andre

von André Labuschagné - am 16.03.2017 15:02
Piet - I tried that but it is not good enough. I specifically do not want the user to see any message and I want the instance running to be made active. That is what I am really looking for. Any ideas?

Andre

von André Labuschagné - am 16.03.2017 15:04
Hello,

I don't know as to how to proceed in case of WinDev but this is the borrowed code that I have been using in my VB6 applications:

Attribute VB_Name = "ForceFore"
' *********************************************************************
' Copyright ©1998 Karl E. Peterson, All Rights Reserved
' http://www.mvps.org/vb
' *********************************************************************
' You are free to use this code within your own applications, but you
' are expressly forbidden from selling or otherwise distributing this
' source code without prior written consent.
' *********************************************************************
Option Explicit
'
' Required Win32 API Declarations
'
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
'
' Constants used with APIs
'
Private Const SW_SHOW = 5
Private Const SW_RESTORE = 9

Public Function ForceForegroundWindow(ByVal hWnd As Long) As Boolean
Dim ThreadID1 As Long
Dim ThreadID2 As Long
Dim nRet As Long
'
' Nothing to do if already in foreground.
'
If hWnd = GetForegroundWindow() Then
ForceForegroundWindow = True
Else
'
' First need to get the thread responsible for this window,
' and the thread for the foreground window.
'
ThreadID1 = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
ThreadID2 = GetWindowThreadProcessId(hWnd, ByVal 0&)
'
' By sharing input state, threads share their concept of
' the active window.
'
If ThreadID1 ThreadID2 Then
Call AttachThreadInput(ThreadID1, ThreadID2, True)
nRet = SetForegroundWindow(hWnd)
Call AttachThreadInput(ThreadID1, ThreadID2, False)
Else
nRet = SetForegroundWindow(hWnd)
End If
'
' Restore and repaint
'
If IsIconic(hWnd) Then
Call ShowWindow(hWnd, SW_RESTORE)
Else
Call ShowWindow(hWnd, SW_SHOW)
End If
'
' SetForegroundWindow return accurately reflects success.
'
ForceForegroundWindow = CBool(nRet)
End If
End Function


HTH

Yogi Yang

von Yogi Yang - am 18.03.2017 06:07
Zur Information:
MySnip.de hat keinen Einfluss auf die Inhalte der Beiträge. Bitte kontaktieren Sie den Administrator des Forums bei Problemen oder Löschforderungen über die Kontaktseite.
Falls die Kontaktaufnahme mit dem Administrator des Forums fehlschlägt, kontaktieren Sie uns bitte über die in unserem Impressum angegebenen Daten.