single instance of app

T

Tim Wallace

I posted this over a week ago and got no response, so here it goes again.

I have an app written for Smartphone using VS 2003. I was under the
impression that "out of the box" a .NETCF app on Smartphone had single
instance enforcement - meaning if your application is running, and the user
attempts to start it again, the original instance is brought to the front.
My application is not doing this. Is my understanding incorrect or is there
something addtional I must do to have my app enforce single instance
functionality?

Tim
 
G

Guest

The Windows Mobile platform should be enforcing singleton app behavior. You
could always use a named mutex to guarantee it yourself (which is what they
should have done in the first place).

-Chris
 
T

Tim Wallace

Chris:

Thanks for your reply. My problem is that the platform isn't enforcing this
behaviour. Do you have any insight as to what may cause this problem? I've
not gone out of my way interrupt the standard process. I basically open a
window with a title bar and a menu. Then, if I press the Home key (thus
going back to the Today screen) and then attempt to relaunch my app, it is
not brought to the top. Do I have to code the "bring to top" myself in
response to an event or something?

Tim
 
G

Guest

So is it launching a second instance, of just not bringing the existing
instance to the fore?

-Chris
 
T

Tim Wallace

Chris:

Actually, I've witnessed two apps running using Task Manager. I added code
to my app to search for my open window and to bring that window to the front
and close the new app. This is only successful in closing the new app. My
existing window is not brought to the fore. Here is the code I added to my
main method to control this issue:

string appname =
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
splash.lMutex = CreateMutex(IntPtr.Zero, true, appname);
if( !lMutex.Equals(new System.IntPtr(0) ) )
{
if(Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS)
{
// need to find the splash window (not posting my actual window
classname or title
IntPtr hFrmMain = FindWindow("{my_window_classname}",
"{my_window_title}");

if(hFrmMain != IntPtr.Zero)
{
// bring that window to front
SetForegroundWindow(hFrmMain);

// exit this application
return;
}
else
{
hFrmMain = IntPtr.Zero;
// check to see if my other window is opened
hFrmMain = FindWindow("{my_other_window_classname}",
"{my_other_window_title}");
if(hFrmMain != IntPtr.Zero)
{
// bring that window to front
SetForegroundWindow(hFrmMain);

// exit this application
return;
}
}
}
}

// run the app (neither the splash window nor the other window are open)
Application.Run(new splash());

Do you see any logic errors?

Tim
 
T

Tim Wallace

Let me add that the code I added will not bring either of my windows to the
top.

Tim
 
T

Tim Wallace

Alex:

I added the 0x1 to the handle (I had to convert the IntPtr to an Int32, do
the |0x1 and convert back to IntPtr). Still, not luck.

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top