Allow only one single process of a Exe

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

Hi all!

I want to code an App that should only run one time at the same time.
How is it possible to find out if this task is allready running.
Any Ideas?

Mike

Tranquilizer/ Secretly!
Registred PureBasic User
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

Find a code snipped http://www.reelmediaproductions.com/pb/ ... nlyone.zip

( now dead Link Fixed )

--

A debugged program is one for which you have not yet found the conditions that make it fail.


Edited by - freak on 10 May 2002 22:18:00

Edited by - freak on 10 May 2002 22:18:24
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
Dead link...

For my own apps, I just test if a window of my app exists, and if so, my app is
therefore already running, and so I abort the new instance. For example, I have
this at the start of my vis2pure app:

Code: Select all

If FindWindow_(0,"vis2pure ")0 : End : EndIf
The space at the end of the name is intentional: it stops a match being found
on the vis2pure disk folder (if open). And yes, it means my app has the space
included in the OpenWindow command.

There are probably better ways to do it, but this way works perfectly fine for me,
especially as it's unlikely that any other app will have the same window title
as mine, and, well, too bad if they do.

PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Freak forgot the .com at the end of the URL... that's why it won't work.

You can browse the entire PB Resources Site by going here...
http://www.reelmediaproductions.com/pb

I've made a few changes to the site. The archives use a small applet to browse though the files, allowing you to search, sort, etc. very quickly and easily.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.

Code: Select all

If FindWindow_(0,"vis2pure ")0 : End : EndIf
The space at the end of the name is intentional: it stops a match being found
on the vis2pure disk folder (if open). And yes, it means my app has the space
included in the OpenWindow command.
I use the same trick. Under Win98 I had no problems, because the title
of the Explorer Window is "Explorer" (followed by the name of the folder).
Under Win2000, however, the word "Explorer" was removed, so the Explorer
window suddenly had the same title as my program...

horst
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
I use the same trick. Under Win98 I had no problems, because the title
of the Explorer Window is "Explorer" (followed by the name of the folder).
Under Win2000, however, the word "Explorer" was removed, so the Explorer
window suddenly had the same title as my program...
Hmm, the space at the end should stop that problem, because no folder can have
a filename ending with a space... I'll have to re-check and see.

PB - Registered PureBasic Coder

Edited by - PB on 10 May 2002 19:13:21
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

The window title is not always the same on Win9x and WinNT+.

And if I recall it right, under WinXP you can choose to have the full 'PATH NAME' or only the 'NAME' in the window title .

Also if somebody renames your application the 'NAME' method doesn't work anymore!

If you want a reliable method for all 'Windows Operating Systems' I would suggest to use my template on Paul's Site.

But it's up to you...


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.
The window title is not always the same on Win9x and WinNT+.

And if I recall it right, under WinXP you can choose to have the full 'PATH NAME' or only the 'NAME' in the window title .

Also if somebody renames your application the 'NAME' method doesn't work anymore!

If you want a reliable method for all 'Windows Operating Systems' I would suggest to use my template on Paul's Site.
My method works on all OSes, even if a user renames my app. Don't be so quick
to write it off... it's a solid tip that I've used for years in Visual Basic.
It only checks for the exe's hard-coded window title and nothing else, so it
doesn't matter what Windows names the folder or Explorer or anything else...

PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.
My method works on all OSes, even if a user renames my app. Don't be so quick
to write it off... it's a solid tip that I've used for years in Visual Basic.
It may work on all OS's but it's certainly not reliable. Some titlebars change the name to match the language you are working in. Some change the name depending on the version you run.

The proper way (and reliable way) is to use Mutex.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

I remember 4 years ago I coded a program that waited as long another program was running, and after that my program went to work.
Searched for this application with 'FindWindow' and it worked very fine - on my machine, running WinNT4.

A coworker of mine wanted to use this program for the same reason/application and it didn't work.
The problem: he worked with Win95 - and the displayed name was NOT the same.
The Problem was that my hard coded title was changed in the 'window title bar' by the OS.
I had to add code to find out which OS was running and adapt the 'FindWindow' routine...

The rest of the story is history


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.

Edited by - franco on 10 May 2002 23:48:07
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

Do you guys block running more than one copy often, because it just sounds like it happens a lot :)

(And why? NB: I want a good reason here, not some lame excuse :wink:

--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.10)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Hi,

I don't know their reasons, but you know there're more than one to try it: not to have several buttons in your task-bar; manage all your docs from the same window; not to have various programs modifying the same ini settings. Just for example.

BTW, I've read that the correct way would be using mutexes, but also that they happen to remain in memory, or something like that. Thank God, there's some people here who know what they talk about (what the hell is a mutex?).

Bye,

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.
The problem: he worked with Win95 - and the displayed name was NOT the same.
The Problem was that my hard coded title was changed in the 'window title bar' by the OS.
That sounds unbelievable. Maybe it was the application program
that (for some reason) changed the title depending on the OS.
I would like to see any evidence that an OS changes the title
string that the programmer has coded.

horst.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> I would like to see any evidence that an OS changes the title
> string that the programmer has coded.

Me too. I think there's a lot of confusion here. What you guys are saying,
is that, for example, an OS can change the title text of PureBasic's editing
window by itself. In other words, if I'm editing a PureBasic app called Test,
and the PureBasic editor is showing PureBasic - Test.pb in the title bar, that
the OS will change this text. Please prove this with an example that I can
reproduce for myself.


PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> why? NB: I want a good reason here, not some lame excuse :wink:

Why block more than one copy? Many reasons. Sometimes an app does a specific
task and if two of it were running, there'd be clashes. For example: An app
that logs something to a text file. If two versions were running, the logfile
would have double entries of the same logged item.


PB - Registered PureBasic Coder
Post Reply