PDA

View Full Version : Allowing only one instance of an autoplay app to be open


jason8
12-29-2005, 02:28 PM
I'm running an autoplay application from within another autoplay application
This is the code im using for the button that opens it.

File.Open("AutoPlay\\Docs\\myapp.exe", "", SW_SHOWNORMAL);

But if the user clicks the button a few times while the app is loading, it ends up opening many instances of the program.

I've tried hiding the button once it has been clicked once. but i am looking for a better solution.

Any ideas on how to only allow one instance of the program to run at any one time?

Thanks
Jason :D

Daniel TM
12-29-2005, 02:42 PM
I'm running an autoplay application from within another autoplay application
This is the code im using for the button that opens it.

File.Open("AutoPlay\\Docs\\myapp.exe", "", SW_SHOWNORMAL);

But if the user clicks the button a few times while the app is loading, it ends up opening many instances of the program.

I've tried hiding the button once it has been clicked once. but i am looking for a better solution.

Any ideas on how to only allow one instance of the program to run at any one time?

Thanks
Jason :D
Try this:
File.Open("AutoPlay\\Docs\\myapp.exe", "AutoPlay\\Docs", SW_SHOWNORMAL);
I'm not sure but it might work.

rhosk
12-29-2005, 02:46 PM
I'm running an autoplay application from within another autoplay application
This is the code im using for the button that opens it.

File.Open("AutoPlay\\Docs\\myapp.exe", "", SW_SHOWNORMAL);

But if the user clicks the button a few times while the app is loading, it ends up opening many instances of the program.

I've tried hiding the button once it has been clicked once. but i am looking for a better solution.

Any ideas on how to only allow one instance of the program to run at any one time?

Thanks
Jason :D

Disable the button when it's clicked.

Gabis
12-29-2005, 02:46 PM
Im very new to all of this, but I think that using the File.IsInUse command should work. something to this effect:

function RunOnceOnly()
CheckRun = File.IsInUse("AutoPlay\\Docs\\myapp.exe");

if CheckRun == false
then
File.Open("AutoPlay\\Docs\\myapp.exe", "", SW_SHOWNORMAL);
end
end

Note: this is really primitive, **** it might not even work properly, but its a start.

edit: And yea, Ron's idea is probably the best. Unless of course something happened that crashed the app and it did need to be run again.

Worm
12-29-2005, 02:58 PM
I'd suggest something like this...

Look in the Project On Startup and Global Functions

Gabis
12-29-2005, 03:09 PM
you could also use Application.Sleep(5000) or something like that. Would make everything unavailable for 5 seconds (at 5000). Just adjust the time to how long you think it should take your app to launch.

You could also use Application.Minimize once the button is pressed and use Application.Restore when it is finished running.

Or do both, if you think someone is going to be pesky enough to try and restore the app just to click the button again =p

jason8
12-29-2005, 03:33 PM
Gabis - You are fantastic!

put this into the button code

CheckRun = File.IsInUse("AutoPlay\\Docs\\myapp.exe");

if CheckRun == false
then
File.Open("AutoPlay\\Docs\\myapp.exe", "", SW_SHOWNORMAL);
end

And it works
Thank you!

Roboblue
12-29-2005, 03:38 PM
This will make the app wait (no actions can be run) in minimized mode until the newly started app is closed. Then the window is restored.

add this to the button On Click

---minimize window to get it out of sight
Window.Minimize(Application.GetWndHandle());
---run the desired app with a wait until closed action
result = File.Run("AutoPlay\\Docs\\myapp.exe", "", "AutoPlay\\Docs", SW_SHOWNORMAL, true);
---restores window when app is closed
Window.Restore(Application.GetWndHandle());

or to hide the window totally

Window.Hide(Application.GetWndHandle());
result = File.Run("AutoPlay\\Docs\\yourapp.exe", "", "AutoPlay\\Docs", SW_SHOWNORMAL, true);
Window.Show(Application.GetWndHandle());

d3m0n1x
11-01-2006, 06:28 PM
I was wondering how u would get this to work if you ran an application and only wanted it to run once.

Had a look at the code in Worms example and tried working in some of the code Gabis provided ...

But the Recipe doesn't exactly work.


CheckRun = File.IsInUse("Portal NetMadeira.exe");
-- call the global function using the window title

bPrevInstance, nHandle = AppPrevInstance("App Prev Instance");

--if there is a previous instance

if CheckRun == false then
--set this window to a small size
Window.SetSize(Application.GetWndHandle(), 0,0)
-- move it out of the screen area
Window.SetPos(Application.GetWndHandle(), -1000, -1000)
-- delete this if you want too
Dialog.Message("Already Running", "App is already running")
-- bring the previous instance to the front.
Window.SetOrder(nHandle, HWND_TOP);
--exit this app
Application.Exit();
end


It won't even let me run the application at all now. :lol

Guess I should leave the spontaneous cooking to the real Chefs and keep to following their recipes.:rolleyes

Am guessing I need to only have it exit if there are 2 instances of the same app open .... or use different code. :huh

Tek
11-02-2006, 10:08 AM
I have this in my startup actions to make sure my application can only run once.


nrunning = 0;
tRunningProcesses = System.EnumerateProcesses();
for pid, file_path in tRunningProcesses do
tpath = String.SplitPath(file_path);
sfilename = tpath.Filename..tpath.Extension;
if sfilename == "iMediaTouchMenu.exe" then
nrunning = nrunning + 1;
end
end
if nrunning > 1 then
result = Dialog.Message("Already Running", "Another instance of iMediaTouch Menu is already running.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
Window.Close(Application.GetWndHandle(), CLOSEWND_TERMINATE)
end

jfxwave
11-24-2006, 09:58 AM
I have this in my startup actions to make sure my application can only run once.


nrunning = 0;
tRunningProcesses = System.EnumerateProcesses();
for pid, file_path in tRunningProcesses do
tpath = String.SplitPath(file_path);
sfilename = tpath.Filename..tpath.Extension;
if sfilename == "iMediaTouchMenu.exe" then
nrunning = nrunning + 1;
end
end
if nrunning > 1 then
result = Dialog.Message("Already Running", "Another instance of iMediaTouch Menu is already running.", MB_OK, MB_ICONEXCLAMATION, MB_DEFBUTTON1);
Window.Close(Application.GetWndHandle(), CLOSEWND_TERMINATE)
end



I like this but if a user changes the name of the program it would not work. I was just thinking about having it check to see if the user changed the name of the app and if so popup a message saying they must change the name back to "whatever your program name is" and if the app matches then run your code to see if it's already running and if any fails exit app.

I was just thinking what would be the best way to check if the user changed the name.
If anyone has an idea please reply.

Eagle
11-24-2006, 10:14 AM
one suggestion is to Settings --> advanced
and set-decide on some 'file version information' for your distributed autorun.exe

adapt some of the code bits above to include file.version information
'matching' and perhaps some myautorun.exe file.getcrc matching aswell ;)

you could also do a search in forum-s using: multi-instance
or similar as the search text

hth a little

Tek
11-24-2006, 10:20 AM
I like this but if a user changes the name of the program it would not work. I was just thinking about having it check to see if the user changed the name of the app and if so popup a message saying they must change the name back to "whatever your program name is" and if the app matches then run your code to see if it's already running and if any fails exit app.

I was just thinking what would be the best way to check if the user changed the name.
If anyone has an idea please reply.

I don't see why a user would change the name of an application since it would break shortcuts, uninstall procedures, etc.

Eagle
11-24-2006, 10:32 AM
I try not to assume anything, when it comes to end users

I include code at runtime that checks the _SourceFolder -install location, crc's etc.
if runtime verifications not 'correct' ..freindly notification- suggesting to re-install etc

(intelligence is built in to all installers-updaters -runtimes to cover these scenarios
as best as possible- what I could think up to dummy-hacker proof ...) ;)

Tek
11-24-2006, 10:38 AM
Well I guess it would depend on the user base, and I know our users would never do that.

TJ_Tigger
11-24-2006, 11:37 AM
There are other ways to do this as well. If you use a registry key to show that the application is open. When your application starts see if the key exists and if it does then exit out of your app. Or you can create a temporary file when your app starts and delete it when your app exits. You could build your app to not start if that file already exists.

The above suggestions do not rely on the name of the file.

Or you could combine multiple methods to have failsafes in place as these sugestions could be tracked by someone running a system tracking program.

You could use the above method using system.EnumerteProcesses but look for the title of the window rather than the name of the exe.

HTH
Tigg