Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I would like to add a code to just run Only one instance of an application

Status
Not open for further replies.

titoneon

MIS
Dec 11, 2009
335
US
Hi,
I have a project file that just consist of a prg file and then i build an exe from the app, but i would like to know if the user just has opened the exe file once and trying to open it again, how to advice that the application is already open, the begining of my prg file is as below, this is in vfp 9.0

_screen.caption=" Find a File... "
_screen.Height=525
_screen.Width=820
_screen.BackColor=(RGB(128,0,255))
_screen.MaxButton = .F.

SET SAFETY OFF
Local oForm As Form
oForm = Createobject('Form1')
oForm.Show(1)
Return

Define Class Form1 As Form
Height = 470
AutoCenter = .T.
Closable = .F.
MinButton = .F.
MaxButton = .F.
Caption = "Search Tool..., To Stop Or cancel Press Escape Key "
Width = 800


Add Object label1 As Label With ;
top = 30, Left = 10, Caption = 'Search File', AutoSize = .T.

Add Object txtFile As TextBox With ;
top = 28, Left = 100, Value = '', Width = 200
an so on

is there a piece of code that i can put at the top for checking that this is already running ?
Thanks
 
There are a number of ways to do this. One very simple one is to attempt to open a DBF EXCLUSIVE. If you succeed, then this is the first instance. If you fail, then you know the application is already running. Just wrap it in a TRY-CATCH and handle appropriately.

Tamar
 
The first thing I do is to NOT launch the EXE itself.

Instead I launch an App Starter which will do a variety of things.

In addition to checking that the user will be running the MOST Recent compilation of the EXE it can also check for the instance of the application running.

Look at: faq184-2442 and/or faq184-1767 and/or faq184-1998 and/or faq184-839 and/or faq184-6206

For an Application Starter you might want to look at: faq184-4492
I am running a modification of that for my clients.

Good Luck,
JRB-Bldr








 
Hi Tamar,
I do not refer to open a DBF file twice, i meant if an exe file is already running and if for any reason a users just double click the exe again, it will be a double instance of the same application, i just wondering, how can i handle it to advise the user that the application is already open or running ?
Thanks
 
I do not refer to open a DBF file twice, i meant if an exe file is already running and if for any reason a users just double click the exe again, it will be a double instance of the same application,

That's exactly what Tamar's suggestion is designed to prevent. The point is that, if the DBF is already open, then that indicates that another instance of the application is already running. The DBF itself is irrelevant. It can just be a dummy file, intended only for the sake of this test.

Having said that, the method I prefer is the first one shown in JRB-Bld's second link (faq184-1767).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Hi,
Sorry Tamar, i miss understood your statement, after Mikes clarification i got it, so i came up with this, i created and startup prg(as JRB-Bldr suggestion not to include in the same app but as a separate ) and this one calls my other prg file wich is the one that will open the form.
so the startup.prg code is as below if you have any other suggestion and think this is not the proper way to do it, i am open to listen, the only thing i would like to do is probably no to display the vfp window when the messagebox() is displayed , here is the code

DECLARE INTEGER CreateEvent IN WIN32API ;
INTEGER lpEventAttributes, ;
SHORT bManualReset, ;
SHORT bInitialState, ;
STRING @ lpName
DECLARE INTEGER GetLastError IN Win32API
DECLARE CloseHandle IN Win32API INTEGER hObject
nEh = CreateEvent(0,0,1, PROGRAM(0) + '.EVENT')
IF GetLastError() = 183 OR nEh = 0 && There is an instance running already (183) the Event can't be *defined release the handle
MESSAGEBOX('The Application is already running, check if it is minimized Press OK to Exit', 0+16, -1)
=CloseHandle(nEh)
QUIT
ENDIF
DO M_FSEARCH

** My M_FSEARCH is the prg that will display the form as i mentione in my first post
 
Hi,
I forgot to say that to hide the main VFP Window i can issue at the top of the startup.prg
Application.Visible = .F.
and then at the bottom before the DO M_FSEARCH
Application.Visible = .T.
Please correct me if i am wrong
Thanks
Ernesto
 
Ernesto,

Personally, I would remove the message box. Given that you are going to make the application's window visible at this point, there seems to be no point in telling the user that it is already running. But that's just my preference. You may well feel differently.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
As Tammar Said "There are a number of ways to do this"

This is another way -

*
* Run Only One Instance of the program
*
if not FirstInstance()
quit
endif

* Program Starts here
* ------------------------------
*
wait window "First " + time()

return

function FirstInstance
#define __FirstInstance .t.
local t_RunOnceDbf, t_Reprocess

t_RunOnceDbf = "runonce.dbf"

if not file(t_RunOnceDbf)
* Create a new table
create dbf &t_RunOnceDbf ( APPDATE d, APPTIME c (10), MACHINEID c(16) )
append blank
use
endif

use &t_RunOnceDbf in 0 alias runmeonce shared
select runmeonce
go top

t_Reprocess = set("reprocess")

set reprocess to 2

if rlock()
replace APPDATE with date(), APPTIME with time()
set reprocess to t_Reprocess
return __FirstInstance
endif

messagebox("Application already running",16,"Warning Message",3000)
return not __FirstInstance
endfunc
 
I'd suggest one small change to NasibKalsi's code.

I would modify this line:

t_RunOnceDbf = "runonce.dbf"

to include a path to the DBF. And I would make that the user's temp folder:

t_RunOnceDbf = FORECEPATH("runonce.dbf", SYS(2023)

That way, you guarantee that the DBF will be local to the user. If you didn't do that, there is the possibility that the DBF will be created in a shared directory, which would result in only one user being able to run the app, which presumably is not what you want.

I'd also get rid of the message box, for the reason I stated earlier. But that's just a matter of personal preference.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Ernesto,

Jrbbldr already pointed you towards many FAQs on this topic.

I'd point out faq184-1998 once more as it has this neat side feature: If the application already runs, instead of informing the user it already runs it just activates the running application. There is no reason to inform the user he has tried to run the application twice. Just seamless activate it to let him see what he'd expect to start anyway.

Then you also are rid of the problem about the _screen showing.

Bye, Olaf.
 
Hey guys
Thanks a lot for all the suggestions, this forum Rock
Ernesto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top