Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.sys.acorn.programmer > #145 > unrolled thread

As it's launched, how can a program test if a copy of itself is already running ?

Started byMartin Hansen <mhh@shrewsbury.org.uk>
First post2011-04-04 00:37 -0700
Last post2011-05-17 20:35 +0100
Articles 15 — 11 participants

Back to article view | Back to comp.sys.acorn.programmer


Contents

  As it's launched, how can a program test if a copy of itself is already running ? Martin Hansen <mhh@shrewsbury.org.uk> - 2011-04-04 00:37 -0700
    Re: As it's launched, how can a program test if a copy of itself is already running ? "John Williams (News)" <UCEbin@tiscali.co.uk> - 2011-04-04 09:59 +0200
    Re: As it's launched, how can a program test if a copy of itself is already running ? James Lampard <dontreplytothis@mailinator.com> - 2011-04-04 01:39 -0700
    Re: As it's launched, how can a program test if a copy of itself is already running ? Dave Higton <davehigton@dsl.pipex.com> - 2011-04-04 20:35 +0100
      Re: As it's launched, how can a program test if a copy of itself is already running ? Martin Hansen <mhh@shrewsbury.org.uk> - 2011-04-05 12:13 -0700
        Re: As it's launched, how can a program test if a copy of itself is already running ? Martin Hansen <mhh@shrewsbury.org.uk> - 2011-04-07 09:47 -0700
    Re: As it's launched, how can a program test if a copy of itself is already running ? Gazza <usenet@garethlock.com> - 2011-04-24 02:58 -0700
      Re: As it's launched, how can a program test if a copy of itself is already running ? Martin <News03@avisoft.f9.co.uk> - 2011-04-24 18:32 +0100
        Re: As it's launched, how can a program test if a copy of itself is already running ? "John Williams (News)" <UCEbin@tiscali.co.uk> - 2011-04-24 20:35 +0200
        Re: As it's launched, how can a program test if a copy of itself is already running ? Alan Adams <alan@adamshome.org.uk> - 2011-04-25 12:29 +0100
          Re: As it's launched, how can a program test if a copy of itself is already running ? Steve Drain <steve@kappa.me.uk> - 2011-05-05 13:01 +0100
            Re: As it's launched, how can a program test if a copy of itself is already running ? Martin Wuerthner <spamtrap@mw-software.com> - 2011-05-05 15:51 +0200
              Re: As it's launched, how can a program test if a copy of itself is already running ? Steve Drain <steve@kappa.me.uk> - 2011-05-05 16:37 +0100
                Re: As it's launched, how can a program test if a copy of itself is already running ? Rick Murray <heyrickmail-usenet@yahoo.co.uk> - 2011-05-06 00:32 +0200
      Re: As it's launched, how can a program test if a copy of itself is Howard Dawson <hsdawson@ormail.co.uk> - 2011-05-17 20:35 +0100

#145 — As it's launched, how can a program test if a copy of itself is already running ?

FromMartin Hansen <mhh@shrewsbury.org.uk>
Date2011-04-04 00:37 -0700
SubjectAs it's launched, how can a program test if a copy of itself is already running ?
Message-ID<9c9556c5-8dd0-498a-b6f5-6aa531f4be63@d28g2000yqf.googlegroups.com>
Dear All,
Let us suppose that program X is running on the desktop.
If a second copy of X is now launched, I'd like it to know that it is
the second copy.
I suspect that there is an easy way of doing this.
Program X is in BBC BASIC, so any code fragments in that language,
please.
Regards,
Martin.

http;//www.RISCOScode.com
http://www.MathMagical.co.uk

[toc] | [next] | [standalone]


#147

From"John Williams (News)" <UCEbin@tiscali.co.uk>
Date2011-04-04 09:59 +0200
Message-ID<51be8d35eeUCEbin@tiscali.co.uk>
In reply to#145
In article
<9c9556c5-8dd0-498a-b6f5-6aa531f4be63@d28g2000yqf.googlegroups.com>,
   Martin Hansen <mhh@shrewsbury.org.uk> wrote:

> Let us suppose that program X is running on the desktop.
> If a second copy of X is now launched, I'd like it to know that it is
> the second copy.
> I suspect that there is an easy way of doing this.
> Program X is in BBC BASIC, so any code fragments in that language,
> please.

My tiny utility TaskCheck may help, and is available from:

        http://irene.williams.free.fr/software/tskchk.htm

It's a bit compact, having been designed down to a minimum size, but is
written in BASIC and should be decipherable.

I imagine it could easily be dapted!

John

-- 
John Williams, Brittany, Northern France - no attachments to these addresses!
Non-RISC OS posters change user to johnrwilliams or put 'risc' in subject!
Who is John Williams? http://petit.four.free.fr/picindex/author/  Somewhere nice to stay in Brittany? http://petit.four.free.fr/visitors/locate

[toc] | [prev] | [next] | [standalone]


#148

FromJames Lampard <dontreplytothis@mailinator.com>
Date2011-04-04 01:39 -0700
Message-ID<95e07756-f531-476e-9002-df8107120741@hg8g2000vbb.googlegroups.com>
In reply to#145
On Apr 4, 8:37 am, Martin Hansen <m...@shrewsbury.org.uk> wrote:
> Dear All,
> Let us suppose that program X is running on the desktop.
> If a second copy of X is now launched, I'd like it to know that it is
> the second copy.
> I suspect that there is an easy way of doing this.
> Program X is in BBC BASIC, so any code fragments in that language,
> please.

I would suggest:

      10 DIM blk% 15
      20
      30 END
      40
      50 DEF FNtask_enum(RETURN context%,RETURN th%,RETURN taskname
$,RETURN
          slot%,RETURN flags%)
      60 SYS "TaskManager_EnumerateTasks",context%,blk%,16 TO context%
      61 IF context% <0 THEN =FALSE
      62 th% =!blk%
      63 SYS "XOS_GenerateError",blk%!4 TO taskname$
      64 slot% =blk%!8
      65 flags% =blk%!12
      66 =TRUE
      70
      80 DEF FNtask_running(task$)
      90 LOCAL context%, taskname$, null%
     100 context% =0
     110 WHILE FNtask_enum(context%,null%,taskname$,null%,null%)
     120   IF taskname$ =task$ THEN =TRUE
     130 ENDWHILE
     140 =FALSE

This works based on the taskname passed to Wimp_Initialise, so
FNtask_running("Zap") will return TRUE if zap is running.

--
James.
To reply use james underline lampard at talk 21 dot com

[toc] | [prev] | [next] | [standalone]


#150

FromDave Higton <davehigton@dsl.pipex.com>
Date2011-04-04 20:35 +0100
Message-ID<88f0ccbe51.davehigton@dsl.pipex.com>
In reply to#145
In message <9c9556c5-8dd0-498a-b6f5-6aa531f4be63@d28g2000yqf.googlegroups.com>
          Martin Hansen <mhh@shrewsbury.org.uk> wrote:

> Dear All,
> Let us suppose that program X is running on the desktop.
> If a second copy of X is now launched, I'd like it to know that it is
> the second copy.
> I suspect that there is an easy way of doing this.
> Program X is in BBC BASIC, so any code fragments in that language,
> please.

Have a look at MBoxAlert on my web site http://www.apts04.dsl.pipex.com
for a complete solution in BASIC (though clearly, what you want is only
a part of my app).

Dave

[toc] | [prev] | [next] | [standalone]


#152

FromMartin Hansen <mhh@shrewsbury.org.uk>
Date2011-04-05 12:13 -0700
Message-ID<0ff67a37-db5c-4444-bfe8-de881031ccf5@d12g2000vbz.googlegroups.com>
In reply to#150
Dear John, James & Dave,
Thanks for the replies.
I'll try and get this sorted tomorrow and let you know how I get on.
Regards,
Martin.

[toc] | [prev] | [next] | [standalone]


#167

FromMartin Hansen <mhh@shrewsbury.org.uk>
Date2011-04-07 09:47 -0700
Message-ID<50dcfd27-c3e5-4082-986b-38fce41c1a63@hd10g2000vbb.googlegroups.com>
In reply to#152
Dear All,
I tried out James' code which works a treat.
Thanks again for the replies.
James, I'll give you a credit in the progam I've used it in.
Thanks,
Martin.

[toc] | [prev] | [next] | [standalone]


#203

FromGazza <usenet@garethlock.com>
Date2011-04-24 02:58 -0700
Message-ID<c2f1bcd8-56dc-4422-a744-1c24bbcdd8c8@l36g2000vbp.googlegroups.com>
In reply to#145
On Apr 4, 8:37 am, Martin Hansen <m...@shrewsbury.org.uk> wrote:
> Dear All,
> Let us suppose that program X is running on the desktop.
> If a second copy of X is now launched, I'd like it to know that it is
> the second copy.
> I suspect that there is an easy way of doing this.
> Program X is in BBC BASIC, so any code fragments in that language,
> please.
> Regards,
> Martin.
>
> http;//www.RISCOScode.comhttp://www.MathMagical.co.uk

Maybe I'm a little bit late here, but wouldn't the easiest way to deal
with this would be to set a system variable to a value, any value,
after the task is setup (1st instance) and check for it's prescence in
the system as each subsequent instance starts.

1. Check Prog$Var
2. IF SET <abort with message and bail> ELSE <set Prog$Var and
continue init seq.>

On exiting the only instance running, you'd just have to remember to
unset the flag variable (Prog$Var in this case.)

It might be a quick and dirty way of doing it, but it always worked
for me, and back in the day there was at least one commercial program
that used this method. Might even have been !Zap, haven't poked around
in the Obey files for ages. I seem to remember it took the form of XXX
$Running (where XXX was the program name.)

[toc] | [prev] | [next] | [standalone]


#208

FromMartin <News03@avisoft.f9.co.uk>
Date2011-04-24 18:32 +0100
Message-ID<51c90e63f1News03@avisoft.f9.co.uk>
In reply to#203
On 24 Apr, in article
<c2f1bcd8-56dc-4422-a744-1c24bbcdd8c8@l36g2000vbp.googlegroups.com>,
   Gazza <usenet@garethlock.com> wrote:
> On Apr 4, 8:37 am, Martin Hansen <m...@shrewsbury.org.uk> wrote:
> > Dear All,
> > Let us suppose that program X is running on the desktop.
> > If a second copy of X is now launched, I'd like it to know that it is
> > the second copy.
> > I suspect that there is an easy way of doing this.
> > Program X is in BBC BASIC, so any code fragments in that language,
> > please.
> > Regards,
> > Martin.
> >
> > http;//www.RISCOScode.comhttp://www.MathMagical.co.uk

> Maybe I'm a little bit late here, but wouldn't the easiest way to deal
> with this would be to set a system variable to a value, any value,
> after the task is setup (1st instance) and check for it's prescence in
> the system as each subsequent instance starts.

> 1. Check Prog$Var
> 2. IF SET <abort with message and bail> ELSE <set Prog$Var and
> continue init seq.>

> On exiting the only instance running, you'd just have to remember to
> unset the flag variable (Prog$Var in this case.)

> It might be a quick and dirty way of doing it, but it always worked
> for me, and back in the day there was at least one commercial program
> that used this method. Might even have been !Zap, haven't poked around
> in the Obey files for ages. I seem to remember it took the form of XXX
> $Running (where XXX was the program name.)

But it is guaranteed to cause users difficulties if there is a problem
after the set that does not do the Unset. 

As you say, quick and nasty!
Much better to check the tasks.

-- 
Martin Avison 
Note that unfortunately this email address will become invalid
without notice if (when) any spam is received. 

[toc] | [prev] | [next] | [standalone]


#209

From"John Williams (News)" <UCEbin@tiscali.co.uk>
Date2011-04-24 20:35 +0200
Message-ID<51c91422b2UCEbin@tiscali.co.uk>
In reply to#208
In article <51c90e63f1News03@avisoft.f9.co.uk>,
   Martin <News03@avisoft.f9.co.uk> wrote:

> Much better to check the tasks.

Omniclient being a case in point!

Have an unforeseen problem (FU) with that and you're scuppered!  Unless you
can be *rs*d messing about with system variables.

Hence my little app TaskCheck:

        http://irene.williams.free.fr/software/tskchk.htm

John

-- 
John Williams, Brittany, Northern France - no attachments to these addresses!
Non-RISC OS posters change user to johnrwilliams or put 'risc' in subject!
Who is John Williams? http://petit.four.free.fr/picindex/author/

[toc] | [prev] | [next] | [standalone]


#210

FromAlan Adams <alan@adamshome.org.uk>
Date2011-04-25 12:29 +0100
Message-ID<1f0771c951.Alan.Adams@laptop.adamshome.org.uk>
In reply to#208
In message <51c90e63f1News03@avisoft.f9.co.uk>
          Martin <News03@avisoft.f9.co.uk> wrote:

> On 24 Apr, in article
> <c2f1bcd8-56dc-4422-a744-1c24bbcdd8c8@l36g2000vbp.googlegroups.com>,
>    Gazza <usenet@garethlock.com> wrote:
>> On Apr 4, 8:37 am, Martin Hansen <m...@shrewsbury.org.uk> wrote:
>>> Dear All,
>>> Let us suppose that program X is running on the desktop.
>>> If a second copy of X is now launched, I'd like it to know that it is
>>> the second copy.
>>> I suspect that there is an easy way of doing this.
>>> Program X is in BBC BASIC, so any code fragments in that language,
>>> please.
<snip>

I have a similar check although its purpose is to prevent multiple 
copies. Code follows.

DEF FNstartwimpcheck(name$, neededversion%)
LOCAL h%
h%=FNfindtask(name$)
IF h%=0 THEN
  PROCstartwimp(name$,neededversion%)
  =TRUE
ELSE
  IF DEBUG% THEN *report name$ is already running - cannot start 
another copy
  =FALSE
ENDIF

REM =========================================== FIND TASK

DEF FNfindtask(name$)

LOCAL ptr%,taskid%,handle%,I%,A$,name%

ptr%=0
taskid%=0
REPEAT
  SYS "TaskManager_EnumerateTasks",taskid%,wlib_block%,16 TO 
taskid%,ptr%
  handle%=!wlib_block%
  name%=wlib_block%!4
  I%=0
  A$=""
  WHILE ( I%<256 ) AND ( (name%?I%) >= 32 )
    A$+=CHR$(name%?I%)
    I%+=1
  ENDWHILE
UNTIL taskid%<0 OR A$=name$
IF A$=name$ THEN =handle% ELSE =0



-- 
Alan Adams, from Northamptonshire
alan@adamshome.org.uk
http://www.nckc.org.uk/

[toc] | [prev] | [next] | [standalone]


#279

FromSteve Drain <steve@kappa.me.uk>
Date2011-05-05 13:01 +0100
Message-ID<Gkwwp.36775$oP6.7868@newsfe19.ams2>
In reply to#210
On 25/04/2011 12:29, Alan Adams wrote:
> In message<51c90e63f1News03@avisoft.f9.co.uk>
>            Martin<News03@avisoft.f9.co.uk>  wrote:
>
>> On 24 Apr, in article
>> <c2f1bcd8-56dc-4422-a744-1c24bbcdd8c8@l36g2000vbp.googlegroups.com>,
>>     Gazza<usenet@garethlock.com>  wrote:
>>> On Apr 4, 8:37 am, Martin Hansen<m...@shrewsbury.org.uk>  wrote:
>>>> Dear All,
>>>> Let us suppose that program X is running on the desktop.
>>>> If a second copy of X is now launched, I'd like it to know that it is
>>>> the second copy.
>>>> I suspect that there is an easy way of doing this.
>>>> Program X is in BBC BASIC, so any code fragments in that language,
>>>> please.
> <snip>
>
> I have a similar check although its purpose is to prevent multiple
> copies. Code follows.
>
> DEF FNstartwimpcheck(name$, neededversion%)
> LOCAL h%
> h%=FNfindtask(name$)
> IF h%=0 THEN
>    PROCstartwimp(name$,neededversion%)
>    =TRUE
> ELSE
>    IF DEBUG% THEN *report name$ is already running - cannot start
> another copy
>    =FALSE
> ENDIF
>
> REM =========================================== FIND TASK
>
> DEF FNfindtask(name$)
>
> LOCAL ptr%,taskid%,handle%,I%,A$,name%
>
> ptr%=0
> taskid%=0
> REPEAT
>    SYS "TaskManager_EnumerateTasks",taskid%,wlib_block%,16 TO
> taskid%,ptr%
>    handle%=!wlib_block%
>    name%=wlib_block%!4
>    I%=0
>    A$=""
>    WHILE ( I%<256 ) AND ( (name%?I%)>= 32 )
>      A$+=CHR$(name%?I%)
>      I%+=1
>    ENDWHILE
> UNTIL taskid%<0 OR A$=name$
> IF A$=name$ THEN =handle% ELSE =0

This is a very late contribution, because I have been away for a month.

I offer it because I think it rather elegant and it is all my own 
invention. ;-)

In the wimp poll loop where you handle messages put these WHEN clauses:

    WHEN &400C2:REM TaskInitialise
     IF $(q%+28)=taskname$ AND q%!4<>taskhandle% THEN
      q%!16=&50400
      SYS"Wimp_SendMessage",17,q%,q%!4
     ENDIF
    WHEN &50400:REM TaskRunning
     PROCquit

* q% is the pointer to the message block
* the message number &50400 comes from a SWI chunk allocated to me, but 
could be any one you have instead. Alternatively, you could use a number 
from the user chunk with bits 19 and 18 set.

[toc] | [prev] | [next] | [standalone]


#280

FromMartin Wuerthner <spamtrap@mw-software.com>
Date2011-05-05 15:51 +0200
Message-ID<ef58a4ce51.martin@bach.planiverse.com>
In reply to#279
In message <Gkwwp.36775$oP6.7868@newsfe19.ams2>
          Steve Drain <steve@kappa.me.uk> wrote:

> This is a very late contribution, because I have been away for a month.

> I offer it because I think it rather elegant and it is all my own
> invention. ;-)

> In the wimp poll loop where you handle messages put these WHEN clauses:

>     WHEN &400C2:REM TaskInitialise
>      IF $(q%+28)=taskname$ AND q%!4<>taskhandle% THEN
>       q%!16=&50400
>       SYS"Wimp_SendMessage",17,q%,q%!4
>      ENDIF
>     WHEN &50400:REM TaskRunning
>      PROCquit

> * q% is the pointer to the message block
> * the message number &50400 comes from a SWI chunk allocated to me, but
> could be any one you have instead. Alternatively, you could use a number
> from the user chunk with bits 19 and 18 set.

Yes, that is indeed very elegant, but why go to the trouble of 
inventing a new message if all you are going to do is quit when seeing 
it? We already have a message that does that, so you might just as 
well send Message_Quit (0) directly. Since your task has a handler for 
that already, you can save the handler for the custom message.

Secondly, the task name at q%+28 is control-terminated, so you should 
avoid using $(q%+28) because that only reads the name correctly if it 
happens to be CR-terminated.

The downside of this approach is that this still causes the second 
task to be fully initialised. It is only quit after reaching its poll 
loop, which might be too late.

-- 
Martin
---------------------------------------------------------------------
Martin Wuerthner         MW Software      http://www.mw-software.com/
        RISC OS Software for Design, Printing and Publishing
---------------------------------------------------------------------

[toc] | [prev] | [next] | [standalone]


#281

FromSteve Drain <steve@kappa.me.uk>
Date2011-05-05 16:37 +0100
Message-ID<Ovzwp.43450$o81.33890@newsfe28.ams2>
In reply to#280
On 05/05/2011 14:51, Martin Wuerthner wrote:
> In message<Gkwwp.36775$oP6.7868@newsfe19.ams2>
>            Steve Drain<steve@kappa.me.uk>  wrote:
>
>> This is a very late contribution, because I have been away for a month.
>
>> I offer it because I think it rather elegant and it is all my own
>> invention. ;-)
>
>> In the wimp poll loop where you handle messages put these WHEN clauses:
>
>>      WHEN&400C2:REM TaskInitialise
>>       IF $(q%+28)=taskname$ AND q%!4<>taskhandle% THEN
>>        q%!16=&50400
>>        SYS"Wimp_SendMessage",17,q%,q%!4
>>       ENDIF
>>      WHEN&50400:REM TaskRunning
>>       PROCquit
>
>> * q% is the pointer to the message block
>> * the message number&50400 comes from a SWI chunk allocated to me, but
>> could be any one you have instead. Alternatively, you could use a number
>> from the user chunk with bits 19 and 18 set.
>
> Yes, that is indeed very elegant, but why go to the trouble of
> inventing a new message if all you are going to do is quit when seeing
> it? We already have a message that does that, so you might just as
> well send Message_Quit (0) directly. Since your task has a handler for
> that already, you can save the handler for the custom message.

The OP wanted to find out if a previous copy was running which is what I 
wrote it for, but because I slipped in that PROCquit you are, of course, 
right.

> Secondly, the task name at q%+28 is control-terminated, so you should
> avoid using $(q%+28) because that only reads the name correctly if it
> happens to be CR-terminated.

Indeed. I took this code directly from a programming system I last did 
anything with in 1998, and then the name was CR terminated for some 
reason that is beyond me now.

> The downside of this approach is that this still causes the second
> task to be fully initialised. It is only quit after reaching its poll
> loop, which might be too late.

That is a problem I recognised, but too late for what?

[toc] | [prev] | [next] | [standalone]


#282

FromRick Murray <heyrickmail-usenet@yahoo.co.uk>
Date2011-05-06 00:32 +0200
Message-ID<4dc3258d$0$30767$ba4acef3@reader.news.orange.fr>
In reply to#281
On 05/05/2011 17:37, Steve Drain wrote:

>> The downside of this approach is that this still causes the second
>> task to be fully initialised. It is only quit after reaching its poll
>> loop, which might be too late.
> That is a problem I recognised, but too late for what?

Consider something like a server that, as part of its initialisation 
cycle, claims ports and stuff external to the program itself.

You want to know if a previous copy is running as soon as possible in 
such an instance, but to wait until the main poll loop.


Best wishes,

Rick.

[toc] | [prev] | [next] | [standalone]


#305 — Re: As it's launched, how can a program test if a copy of itself is

FromHoward Dawson <hsdawson@ormail.co.uk>
Date2011-05-17 20:35 +0100
SubjectRe: As it's launched, how can a program test if a copy of itself is
Message-ID<na.a3f1ec51d4.a80520hsdawson@ormail.co.uk>
In reply to#203
In article
<c2f1bcd8-56dc-4422-a744-1c24bbcdd8c8@l36g2000vbp.googlegroups.com>, Gazza
<usenet@garethlock.com> wrote:
om>It might be a quick and dirty way of doing it, but it always worked
om>for me, and back in the day there was at least one commercial program
om>that used this method. Might even have been !Zap, haven't poked around
om>in the Obey files for ages. I seem to remember it took the form of XXX
om>$Running (where XXX was the program name.)
om>
om>

It's OK unless the program crashes or you exit using Alt-Break. In these
cases the sysvar cannot be automatically reset and you will be prevented
from restarting the program unless you reboot or *Unset the sysvar manually.

I found it's well worth grasping the nettle once and doing it by fishing
around for the relevant application's name in the block returned by the swi
TaskManager_EnumerateTasks. You end up with a very useful general function
that cannot be fooled even by crashes or Alt-breaks.

Howard.

-- 
-------------
Howard Dawson
 
hsdawson@ormail.co.uk

http://hd-audio.orpheusweb.co.uk/
---------------------------------

[toc] | [prev] | [standalone]


Back to top | Article view | comp.sys.acorn.programmer


csiph-web