Jouni1979

hi,

I am developing software¡¯s for Psion Work about pro devices. I am using CF 2.0. I have this tiny problem. I don¡¯t know how to prevent program starting if it is already on. I heard that mutex are one way to do it, but I don¡¯t know how




Re: .NET Compact Framework How to prevent software startup a twice.

AlexFeinman

A form-based application will not start twice. The CF runtime contains code that automatically detects a running instance of the same application, and activates it instead






Re: .NET Compact Framework How to prevent software startup a twice.

James Berry

What is the mechanism it uses to do this

I have a forms based cf 2.0 application which users are sometimes able to open twice, and I can't get to the bottom of it.

Initially I thought it was because I had a lot of startup processing before my first form showed and users were clicking on the program many times, but I reduced this somewhat. My program does show and hide multiple forms for performance rather than destroying them - can this mechanism get confused by having many forms

Best wishes
James





Re: .NET Compact Framework How to prevent software startup a twice.

Ilya Tumanov

NETCF checks if window with special name is present. That works well but only if:

- Your platform is Windows Mobile (no check in CE as it allows for multiple instances).

- Your application is GUI application (not console).

- Application.Run() is invoked right away.

If that's not the case then implement your own singleton behavior. Most common is to create named mutex and check if it exists. Please search forums for more info on that or a code sample.






Re: .NET Compact Framework How to prevent software startup a twice.

James Berry

Ilya

Many thanks for this - since I don't call Application.Run straight away, that is likely to be the problem. I can handle the Mutex part, but how can I duplicate the behaviour of bringing the existing running application to the front My users are not very good at navigating Windows Mobile and find the easiest way to find the application is just to "re-launch" it...

Best wishes
James





Re: .NET Compact Framework How to prevent software startup a twice.

Christopher Fairbairn

Hi James,

James Berry wrote:
Many thanks for this - since I don't call Application.Run straight away, that is likely to be the problem. I can handle the Mutex part, but how can I duplicate the behaviour of bringing the existing running application to the front

How you do this is a little application specific. The general concept is to somehow find the window handle of the other application's main window and then bring that to the foreground.

If you PInvoke the native FindWindow and SetForegroundWindow APIs, one approach may be something like the following:

Code Block
IntPtr hOtherWnd = FindWindow("classname", "windowname");
if (hOtherWnd)
SetForegroundWindow(hOtherWnd);

FindWindow is documented on MSDN at http://msdn2.microsoft.com/en-us/library/ms960613.aspx. The "classname" and "windowname" strings would be dependant upon your application. Since the .NET Compact Framework (between different versions) has changed the classname used for windows, I would possibly suggest passing in null for the first parameter to FindWindow.

Hope this helps,

Christopher Fairbairn






Re: .NET Compact Framework How to prevent software startup a twice.

gnpaolo

My 2 cents.

I have a NETCF Form based application for Windows Mobile 6, calling Application.Run() right away.

I have one single link to the application in the Startup folder.

Everything works okay on every device I tested it, apart from the HTC S710 device:

in this device I found my application started twice.





Re: .NET Compact Framework How to prevent software startup a twice.

Chunsheng Tang - MSFT

Hi,

Here a code sample for a single instance application using a named mutex:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=145477&SiteID=1

Another code sample using an event object:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=724870&SiteID=1

Best Regards
Chunsheng Tang





Re: .NET Compact Framework How to prevent software startup a twice.

James Berry

The code extract in the second link solves my problem in testing. I'll roll it out across our devices and see if we get any more problems in the field.

Thank you!

Best wishes
James