It is currently Sat Aug 24, 2019 6:24 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Exe instance question
PostPosted: Wed May 07, 2003 9:15 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Apr 25, 2003 7:52 pm
Posts: 124
Location: Netherlands
I have the following situation.

Start test.exe

Start the same program test.exe for the second time

The first instance must be closed and the second must be opened.

Is there a way to do this?

_________________
Cor de Visser

Registered PureBasic user

Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed May 07, 2003 9:34 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5780
Location: Germany
If I remember right, there was a snipped on the Recource Site that does
that. Have a search there, maybe you find it.

You can also have a look at the source of the Editor. It behaves in a
similar way (if a second instance is opened, the second one get's closed)

Timo

_________________
quidquid Latine dictum sit altum videtur


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed May 07, 2003 9:37 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Apr 25, 2003 7:52 pm
Posts: 124
Location: Netherlands
Thanks Freak

_________________
Cor de Visser

Registered PureBasic user

Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed May 07, 2003 9:44 pm 
Offline
Administrator
Administrator

Joined: Wed Apr 23, 2003 7:57 pm
Posts: 1321
Location: Belgium
Here an small example
Code:
ERROR_ALREADY_EXISTS = 183
Global WM_ACTIVATEOLDINST
WM_ACTIVATEOLDINST = RegisterWindowMessage_("WM_ACTIVATEOLDINST_PB")
MutexName$ = "mylittletestapp"

Procedure MyWindowCallback(WindowID, Message, wParam, lParam) 
  Result = #PB_ProcessPureBasicEvents 
  If message = WM_ACTIVATEOLDINST
    CloseHandle_(hMutex)
    OpenConsole()
    PrintN("closing app")
    Delay(2000)
    End
  EndIf
  ProcedureReturn Result 
EndProcedure 

hMutex = CreateMutex_(0, 0, @MutexName$)
If GetLastError_() = ERROR_ALREADY_EXISTS
  SendMessage_(#HWND_BROADCAST, WM_ACTIVATEOLDINST, 0, 0)
  Delay(100)
EndIf


If OpenWindow(0,100,150,450,200,#PB_Window_SystemMenu,"MyLittleTestApp " + Str(Random(500)))
  SetWindowCallback(@MyWindowCallback())
  Repeat
    ev=WaitWindowEvent()
  Until ev=#PB_EventCloseWindow
EndIf

If hMutex <> 0
 CloseHandle_(hMutex)
EndIf

End

_________________
Berikco

http://www.vispluk.be


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu May 08, 2003 2:06 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Fri Apr 25, 2003 5:24 pm
Posts: 7581
> Here an small example

Here's an even smaller example: :twisted:

Code:
; These two lines prevent this app from being run more than once.
; You must put these lines at the start of your app's code (obviously).
MutexID=CreateMutex_(0,1,"YourAppName") : MutexError=GetLastError_()
If MutexID=0 Or MutexError<>0 : ReleaseMutex_(MutexID) : CloseHandle_(MutexID) : End : EndIf
;
; Your app's code now goes here, until quitting time, which executes the next line.
;
CloseHandle_(MutexID) : End


Top
 Profile  
Reply with quote  
 Post subject: Re: Exe instance question
PostPosted: Thu May 08, 2003 7:56 am 
Offline
Administrator
Administrator

Joined: Wed Apr 23, 2003 7:57 pm
Posts: 1321
Location: Belgium
PB wrote:
> Here an small example

Here's an even smaller example:


Cor wrote:
The first instance must be closed and the second must be opened.


No PB, you did not read the whole question, to fast again.
First closed.....second....
:mrgreen: :mrgreen: :mrgreen:

_________________
Berikco

http://www.vispluk.be


Top
 Profile  
Reply with quote  
 Post subject: Re: Exe instance question
PostPosted: Thu May 08, 2003 9:49 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Fri Apr 25, 2003 5:24 pm
Posts: 7581
> No PB, you did not read the whole question, to fast again.
> First closed.....second....

Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Aug 03, 2003 3:59 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Aug 03, 2003 12:06 am
Posts: 264
Location: Austria
Just for curiosity ...

What situation could make it worth to start the same program twice only to close the first one immediately after you started the second one?
Or did I miss the point somehow?
I mean: what can the same program do at its second start what it cannot do at first try?

_________________
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Mar 11, 2004 1:00 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Mar 07, 2004 8:47 am
Posts: 1857
Location: Argentina
Hmm Dunno! maybe his proggy is a trial release and each time you open it you have less runs to go ? :wink: Just wondering heh..


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Jun 16, 2005 12:48 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2005 5:05 pm
Posts: 1769
Location: Norway
Here is a modification of the above code!
It let you do single instance running just like any "good" application!

Run the program, minimize the window. Then run it again!
If all goes well the program won't start again,
but instead message the old instance and restore it and bring it to the foreground!

Only tested on XP Pro, but should behave in a similar way on all windows versions. not entirely sure if W95 behave exactly the same or not, test to be sure!

Code:
ERROR_ALREADY_EXISTS = 183
Global WM_ACTIVATEOLDINST
WM_ACTIVATEOLDINST = RegisterWindowMessage_("WM_ACTIVATEOLDINST_PB")
MutexName$ = "mylittletestapp"

Procedure MyWindowCallback(WindowID, message, wParam, lParam)
 Result = #PB_ProcessPureBasicEvents
 If message = WM_ACTIVATEOLDINST
  ShowWindow_(WindowID,#SW_RESTORE)
  SetForegroundWindow_(WindowID)
  EndIf
 ProcedureReturn Result
EndProcedure

hMutex = CreateMutex_(0, 0, @MutexName$)
If GetLastError_() = ERROR_ALREADY_EXISTS
  SendMessage_(#HWND_BROADCAST, WM_ACTIVATEOLDINST, 0, 0)
  End
EndIf


hwnd=OpenWindow(0,100,150,450,200,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget,"MyLittleTestApp ")
If hwnd
  SetWindowCallback(@MyWindowCallback())
  Repeat
   ev=WaitWindowEvent()
   If showoldwindow
   EndIf
  Until ev=#PB_EventCloseWindow
EndIf

If hMutex <> 0
 CloseHandle_(hMutex)
EndIf

End


I'll leave it up to you as a coding exercise to find out how to send a message to the old (first) app with a filepath. (like many editors do)
Shouldn't be hard, the messaging "example" is already here so. Hehe!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye