Avoid double execution of a program

  • Thread starter Thread starter Saverio Tedeschi
  • Start date Start date
S

Saverio Tedeschi

Hi all gurus,
forgive me if I'm bothering you. My .NET CF app is designoed to be executed
once in target PDAs. However, in customers' hands (actually, refereees) it
seems they are able to spawn app more than once, as I receive errors on
table locking and replying "OK" the app opens the table, while the design is
to loop in opening retries, and a certain numbers of little (?)
malfunctioning routines like this one. Even if this (most time) does not
lock the execution, there error messages are annoying and, the most
important thing, confusing. I guess that in the time the app performs some
inizialization, users are apple to launch it a couple of times, tapping on
HW button associated. How can I avoid this (kkep in mind that for a couple
of seconds nothing appear on PDA's screen. Can some1 point me to the right
direction? TIA!
 
You need to grab a named mutex at app startup. If it already exists, the
app is already running so exit. Search the archives for CreateMutex and/or
single instance.

-Chris
 
Hi Chris,
thank you 4 your reply. What about CreateSemaphore function? Easier,
tricker, harder? TIA
 
Thank you, Chris, for your reply.
Useless to say, I'm moving my first steps into inter-program and inter-task
syncronization, so pls forgive me if I'm bothering you.
However, I can't find how2 create a named mutex in VS 2003 (CF 1.0 SP2), as
there's only an "Already owned" parameter in New function. Should I create a
Win32-wrapper? Where can I find C# or VB declaration for PPC2002/2003
targets? TIA
 
Thank you Matt, 4 your suggestion.
My problem is, I'm not able to call CreateMutex from inside my code, as .NET
1.1 only allows 1 parameter (Hold) and I don't know parameters requested by
Win32 function not in what dll it is, to build a wrapper. If you could me
help me in this... LOT anyway.
 
Thank you Sergey, for your reply.
The problem is, referees try to call program twice (or three times, or four)
before it can display a form, for it needs to do some housekeeping. I
immediately spawn a secondary task to display splash screen, but it has not
the name of the main form, and more, it closes by itself after 10 seconds
(housekeeping takes abot 12 secs on a 200MHz XScale processor, or on a 400
forced to 200 for battery-lasting purposes). I appreciate your clue anyway.
 
The example demonstrates how to use named event and does not depend from
the main form:

IntPtr evnt = CreateEvent(IntPtr.Zero, false, false, AppNamedEvent);
if (Marshal.GetLastWin32Error() != 0)
{
// one instance of application is run, activate it
IntPtr hwnd = FindWindow(null, "Form1");
if (hwnd != IntPtr.Zero)
{
SetForegroundWindow(hwnd);
}

return;
}

As you can see it will activate the main form if it exists and exit all
the same.
 
Hi Sergey;
CreateEvent is not supported in my version of CF, as only GetEvents and
GetProperties are supported as methods of
System.ComponentModel.TypeDescriptor. Did I take the wrong way or am I
overlooking something? TIA.
 
I'd better put an hammer on my fingers... maybe after I've written this
reply and my thanks...

Sergey Bogdanov said:
Yep, carefully see the example [1]:- CreateEvent is:

[DllImport("coredll.dll", EntryPoint="CreateEvent", SetLastError = true)]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool
bManualReset, bool bInitialState, string lpName);

[1] http://www.sergeybogdanov.com/Samples/SPSingleInstance.zip


--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio said:
Hi Sergey;
CreateEvent is not supported in my version of CF, as only GetEvents and
GetProperties are supported as methods of
System.ComponentModel.TypeDescriptor. Did I take the wrong way or am I
overlooking something? TIA.
 
Hi;
I'd like to understand, if you can spend a little more time, the difference
between this method and CreateMutex (named). TIA.

Sergey Bogdanov said:
Yep, carefully see the example [1]:- CreateEvent is:

[DllImport("coredll.dll", EntryPoint="CreateEvent", SetLastError = true)]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool
bManualReset, bool bInitialState, string lpName);

[1] http://www.sergeybogdanov.com/Samples/SPSingleInstance.zip


--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio said:
Hi Sergey;
CreateEvent is not supported in my version of CF, as only GetEvents and
GetProperties are supported as methods of
System.ComponentModel.TypeDescriptor. Did I take the wrong way or am I
overlooking something? TIA.
 
The difference is one uses an event, the other uses a mutex. Both are
common sychronization objects and are well documented. It's primarily a
style difference, but for this I'd opt for the Mutex as it's a single call
with no need for listeners.

-Chris

Saverio Tedeschi said:
Hi;
I'd like to understand, if you can spend a little more time, the
difference between this method and CreateMutex (named). TIA.

Sergey Bogdanov said:
Yep, carefully see the example [1]:- CreateEvent is:

[DllImport("coredll.dll", EntryPoint="CreateEvent", SetLastError = true)]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool
bManualReset, bool bInitialState, string lpName);

[1] http://www.sergeybogdanov.com/Samples/SPSingleInstance.zip


--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio said:
Hi Sergey;
CreateEvent is not supported in my version of CF, as only GetEvents and
GetProperties are supported as methods of
System.ComponentModel.TypeDescriptor. Did I take the wrong way or am I
overlooking something? TIA.

"Sergey Bogdanov" <[email protected]> ha scritto nel messaggio

The example demonstrates how to use named event and does not depend from
the main form:

IntPtr evnt = CreateEvent(IntPtr.Zero, false, false, AppNamedEvent);
if (Marshal.GetLastWin32Error() != 0)
{
// one instance of application is run, activate it
IntPtr hwnd = FindWindow(null, "Form1");
if (hwnd != IntPtr.Zero)
{
SetForegroundWindow(hwnd);
}

return;
}

As you can see it will activate the main form if it exists and exit all
the same.

--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:

Thank you Sergey, for your reply.
The problem is, referees try to call program twice (or three times, or
four) before it can display a form, for it needs to do some
housekeeping. I immediately spawn a secondary task to display splash
screen, but it has not the name of the main form, and more, it closes
by itself after 10 seconds (housekeeping takes abot 12 secs on a 200MHz
XScale processor, or on a 400 forced to 200 for battery-lasting
purposes). I appreciate your clue anyway.

"Sergey Bogdanov" <[email protected]> ha scritto nel messaggio


Here is an example how to create a named event to accomplish the
single instance behaviour for your application in SmartPhone. This
code snippet should work for PPC either:
http://www.sergeybogdanov.com/Samples/SPSingleInstance.zip


--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:


Thank you, Chris, for your reply.
Useless to say, I'm moving my first steps into inter-program and
inter-task syncronization, so pls forgive me if I'm bothering you.
However, I can't find how2 create a named mutex in VS 2003 (CF 1.0
SP2), as there's only an "Already owned" parameter in New function.
Should I create a Win32-wrapper? Where can I find C# or VB
declaration for PPC2002/2003 targets? TIA

"Chris Tacke, eMVP" <[email protected]> ha scritto nel
messaggio


A semaphore is not something useful in this case. A mutex is, hence
the recommendation.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate





Hi Chris,
thank you 4 your reply. What about CreateSemaphore function?
Easier, tricker, harder? TIA

"<ctacke/>" <ctacke_AT_OpenNETCF_com> ha scritto nel messaggio



You need to grab a named mutex at app startup. If it already
exists, the app is already running so exit. Search the archives
for CreateMutex and/or single instance.

-Chris





Hi all gurus,
forgive me if I'm bothering you. My .NET CF app is designoed to
be executed once in target PDAs. However, in customers' hands
(actually, refereees) it seems they are able to spawn app more
than once, as I receive errors on table locking and replying "OK"
the app opens the table, while the design is to loop in opening
retries, and a certain numbers of little (?) malfunctioning
routines like this one. Even if this (most time) does not lock
the execution, there error messages are annoying and, the most
important thing, confusing. I guess that in the time the app
performs some inizialization, users are apple to launch it a
couple of times, tapping on HW button associated. How can I avoid
this (kkep in mind that for a couple of seconds nothing appear on
PDA's screen. Can some1 point me to the right direction? TIA!
 
Nevertheless, I believe that there are no differences between mutex and
named event in that case. I just try to create a named event and if
there was an error (the event already exists) then just stop execution.

IntPtr event = CreateEvent(IntPtr.Zero, false, false, AppNamedEvent);
if (Marshal.GetLastWin32Error() != 0)
{
return;
}


--
Sergey Bogdanov
http://www.sergeybogdanov.com


The difference is one uses an event, the other uses a mutex. Both are
common sychronization objects and are well documented. It's primarily a
style difference, but for this I'd opt for the Mutex as it's a single call
with no need for listeners.

-Chris

Hi;
I'd like to understand, if you can spend a little more time, the
difference between this method and CreateMutex (named). TIA.

Yep, carefully see the example [1]:- CreateEvent is:

[DllImport("coredll.dll", EntryPoint="CreateEvent", SetLastError = true)]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool
bManualReset, bool bInitialState, string lpName);

[1] http://www.sergeybogdanov.com/Samples/SPSingleInstance.zip


--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:

Hi Sergey;
CreateEvent is not supported in my version of CF, as only GetEvents and
GetProperties are supported as methods of
System.ComponentModel.TypeDescriptor. Did I take the wrong way or am I
overlooking something? TIA.

"Sergey Bogdanov" <[email protected]> ha scritto nel messaggio


The example demonstrates how to use named event and does not depend from
the main form:

IntPtr evnt = CreateEvent(IntPtr.Zero, false, false, AppNamedEvent);
if (Marshal.GetLastWin32Error() != 0)
{
// one instance of application is run, activate it
IntPtr hwnd = FindWindow(null, "Form1");
if (hwnd != IntPtr.Zero)
{
SetForegroundWindow(hwnd);
}

return;
}

As you can see it will activate the main form if it exists and exit all
the same.

--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:


Thank you Sergey, for your reply.
The problem is, referees try to call program twice (or three times, or
four) before it can display a form, for it needs to do some
housekeeping. I immediately spawn a secondary task to display splash
screen, but it has not the name of the main form, and more, it closes
by itself after 10 seconds (housekeeping takes abot 12 secs on a 200MHz
XScale processor, or on a 400 forced to 200 for battery-lasting
purposes). I appreciate your clue anyway.

"Sergey Bogdanov" <[email protected]> ha scritto nel messaggio



Here is an example how to create a named event to accomplish the
single instance behaviour for your application in SmartPhone. This
code snippet should work for PPC either:
http://www.sergeybogdanov.com/Samples/SPSingleInstance.zip


--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:



Thank you, Chris, for your reply.
Useless to say, I'm moving my first steps into inter-program and
inter-task syncronization, so pls forgive me if I'm bothering you.
However, I can't find how2 create a named mutex in VS 2003 (CF 1.0
SP2), as there's only an "Already owned" parameter in New function.
Should I create a Win32-wrapper? Where can I find C# or VB
declaration for PPC2002/2003 targets? TIA

"Chris Tacke, eMVP" <[email protected]> ha scritto nel
messaggio



A semaphore is not something useful in this case. A mutex is, hence
the recommendation.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate






Hi Chris,
thank you 4 your reply. What about CreateSemaphore function?
Easier, tricker, harder? TIA

"<ctacke/>" <ctacke_AT_OpenNETCF_com> ha scritto nel messaggio




You need to grab a named mutex at app startup. If it already
exists, the app is already running so exit. Search the archives
for CreateMutex and/or single instance.

-Chris






Hi all gurus,
forgive me if I'm bothering you. My .NET CF app is designoed to
be executed once in target PDAs. However, in customers' hands
(actually, refereees) it seems they are able to spawn app more
than once, as I receive errors on table locking and replying "OK"
the app opens the table, while the design is to loop in opening
retries, and a certain numbers of little (?) malfunctioning
routines like this one. Even if this (most time) does not lock
the execution, there error messages are annoying and, the most
important thing, confusing. I guess that in the time the app
performs some inizialization, users are apple to launch it a
couple of times, tapping on HW button associated. How can I avoid
this (kkep in mind that for a couple of seconds nothing appear on
PDA's screen. Can some1 point me to the right direction? TIA!
 
Both work like a charm!
Thank you so much, guys. Besides your expertise, I admire your patience as
well. God bless you!

Sergey Bogdanov said:
Nevertheless, I believe that there are no differences between mutex and
named event in that case. I just try to create a named event and if there
was an error (the event already exists) then just stop execution.

IntPtr event = CreateEvent(IntPtr.Zero, false, false, AppNamedEvent);
if (Marshal.GetLastWin32Error() != 0)
{
return;
}


--
Sergey Bogdanov
http://www.sergeybogdanov.com


The difference is one uses an event, the other uses a mutex. Both are
common sychronization objects and are well documented. It's primarily a
style difference, but for this I'd opt for the Mutex as it's a single
call with no need for listeners.

-Chris

Hi;
I'd like to understand, if you can spend a little more time, the
difference between this method and CreateMutex (named). TIA.

"Sergey Bogdanov" <[email protected]> ha scritto nel messaggio

Yep, carefully see the example [1]:- CreateEvent is:

[DllImport("coredll.dll", EntryPoint="CreateEvent", SetLastError =
true)]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool
bManualReset, bool bInitialState, string lpName);

[1] http://www.sergeybogdanov.com/Samples/SPSingleInstance.zip


--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:

Hi Sergey;
CreateEvent is not supported in my version of CF, as only GetEvents and
GetProperties are supported as methods of
System.ComponentModel.TypeDescriptor. Did I take the wrong way or am I
overlooking something? TIA.

"Sergey Bogdanov" <[email protected]> ha scritto nel messaggio


The example demonstrates how to use named event and does not depend
from the main form:

IntPtr evnt = CreateEvent(IntPtr.Zero, false, false, AppNamedEvent);
if (Marshal.GetLastWin32Error() != 0)
{
// one instance of application is run, activate it
IntPtr hwnd = FindWindow(null, "Form1");
if (hwnd != IntPtr.Zero)
{
SetForegroundWindow(hwnd);
}

return;
}

As you can see it will activate the main form if it exists and exit
all the same.

--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:


Thank you Sergey, for your reply.
The problem is, referees try to call program twice (or three times,
or four) before it can display a form, for it needs to do some
housekeeping. I immediately spawn a secondary task to display splash
screen, but it has not the name of the main form, and more, it closes
by itself after 10 seconds (housekeeping takes abot 12 secs on a
200MHz XScale processor, or on a 400 forced to 200 for
battery-lasting purposes). I appreciate your clue anyway.

"Sergey Bogdanov" <[email protected]> ha scritto nel
messaggio


Here is an example how to create a named event to accomplish the
single instance behaviour for your application in SmartPhone. This
code snippet should work for PPC either:
http://www.sergeybogdanov.com/Samples/SPSingleInstance.zip


--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:



Thank you, Chris, for your reply.
Useless to say, I'm moving my first steps into inter-program and
inter-task syncronization, so pls forgive me if I'm bothering you.
However, I can't find how2 create a named mutex in VS 2003 (CF 1.0
SP2), as there's only an "Already owned" parameter in New function.
Should I create a Win32-wrapper? Where can I find C# or VB
declaration for PPC2002/2003 targets? TIA

"Chris Tacke, eMVP" <[email protected]> ha scritto nel
messaggio



A semaphore is not something useful in this case. A mutex is,
hence the recommendation.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate






Hi Chris,
thank you 4 your reply. What about CreateSemaphore function?
Easier, tricker, harder? TIA

"<ctacke/>" <ctacke_AT_OpenNETCF_com> ha scritto nel messaggio




You need to grab a named mutex at app startup. If it already
exists, the app is already running so exit. Search the archives
for CreateMutex and/or single instance.

-Chris






Hi all gurus,
forgive me if I'm bothering you. My .NET CF app is designoed to
be executed once in target PDAs. However, in customers' hands
(actually, refereees) it seems they are able to spawn app more
than once, as I receive errors on table locking and replying
"OK" the app opens the table, while the design is to loop in
opening retries, and a certain numbers of little (?)
malfunctioning routines like this one. Even if this (most time)
does not lock the execution, there error messages are annoying
and, the most important thing, confusing. I guess that in the
time the app performs some inizialization, users are apple to
launch it a couple of times, tapping on HW button associated.
How can I avoid this (kkep in mind that for a couple of seconds
nothing appear on PDA's screen. Can some1 point me to the right
direction? TIA!
 
Agreed. THere's nothing wrong with either approach. It's simply a style
decision.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate


Sergey Bogdanov said:
Nevertheless, I believe that there are no differences between mutex and
named event in that case. I just try to create a named event and if there
was an error (the event already exists) then just stop execution.

IntPtr event = CreateEvent(IntPtr.Zero, false, false, AppNamedEvent);
if (Marshal.GetLastWin32Error() != 0)
{
return;
}


--
Sergey Bogdanov
http://www.sergeybogdanov.com


The difference is one uses an event, the other uses a mutex. Both are
common sychronization objects and are well documented. It's primarily a
style difference, but for this I'd opt for the Mutex as it's a single
call with no need for listeners.

-Chris

Hi;
I'd like to understand, if you can spend a little more time, the
difference between this method and CreateMutex (named). TIA.

"Sergey Bogdanov" <[email protected]> ha scritto nel messaggio

Yep, carefully see the example [1]:- CreateEvent is:

[DllImport("coredll.dll", EntryPoint="CreateEvent", SetLastError =
true)]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool
bManualReset, bool bInitialState, string lpName);

[1] http://www.sergeybogdanov.com/Samples/SPSingleInstance.zip


--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:

Hi Sergey;
CreateEvent is not supported in my version of CF, as only GetEvents and
GetProperties are supported as methods of
System.ComponentModel.TypeDescriptor. Did I take the wrong way or am I
overlooking something? TIA.

"Sergey Bogdanov" <[email protected]> ha scritto nel messaggio


The example demonstrates how to use named event and does not depend
from the main form:

IntPtr evnt = CreateEvent(IntPtr.Zero, false, false, AppNamedEvent);
if (Marshal.GetLastWin32Error() != 0)
{
// one instance of application is run, activate it
IntPtr hwnd = FindWindow(null, "Form1");
if (hwnd != IntPtr.Zero)
{
SetForegroundWindow(hwnd);
}

return;
}

As you can see it will activate the main form if it exists and exit
all the same.

--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:


Thank you Sergey, for your reply.
The problem is, referees try to call program twice (or three times,
or four) before it can display a form, for it needs to do some
housekeeping. I immediately spawn a secondary task to display splash
screen, but it has not the name of the main form, and more, it closes
by itself after 10 seconds (housekeeping takes abot 12 secs on a
200MHz XScale processor, or on a 400 forced to 200 for
battery-lasting purposes). I appreciate your clue anyway.

"Sergey Bogdanov" <[email protected]> ha scritto nel
messaggio


Here is an example how to create a named event to accomplish the
single instance behaviour for your application in SmartPhone. This
code snippet should work for PPC either:
http://www.sergeybogdanov.com/Samples/SPSingleInstance.zip


--
Sergey Bogdanov
http://www.sergeybogdanov.com


Saverio Tedeschi wrote:



Thank you, Chris, for your reply.
Useless to say, I'm moving my first steps into inter-program and
inter-task syncronization, so pls forgive me if I'm bothering you.
However, I can't find how2 create a named mutex in VS 2003 (CF 1.0
SP2), as there's only an "Already owned" parameter in New function.
Should I create a Win32-wrapper? Where can I find C# or VB
declaration for PPC2002/2003 targets? TIA

"Chris Tacke, eMVP" <[email protected]> ha scritto nel
messaggio



A semaphore is not something useful in this case. A mutex is,
hence the recommendation.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate






Hi Chris,
thank you 4 your reply. What about CreateSemaphore function?
Easier, tricker, harder? TIA

"<ctacke/>" <ctacke_AT_OpenNETCF_com> ha scritto nel messaggio




You need to grab a named mutex at app startup. If it already
exists, the app is already running so exit. Search the archives
for CreateMutex and/or single instance.

-Chris






Hi all gurus,
forgive me if I'm bothering you. My .NET CF app is designoed to
be executed once in target PDAs. However, in customers' hands
(actually, refereees) it seems they are able to spawn app more
than once, as I receive errors on table locking and replying
"OK" the app opens the table, while the design is to loop in
opening retries, and a certain numbers of little (?)
malfunctioning routines like this one. Even if this (most time)
does not lock the execution, there error messages are annoying
and, the most important thing, confusing. I guess that in the
time the app performs some inizialization, users are apple to
launch it a couple of times, tapping on HW button associated.
How can I avoid this (kkep in mind that for a couple of seconds
nothing appear on PDA's screen. Can some1 point me to the right
direction? TIA!
 
Back
Top