Solved

Test if application is already running

Posted on 2009-07-03
3
205 Views
Last Modified: 2013-12-27
I would like to check if there is already running an instance of program on windows mobile device, so I could prevent multiple lunches of the program.
0
Comment
Question by:uveper
  • 2
3 Comments
 
LVL 24

Expert Comment

by:alexey_gusev
ID: 24770573
with cf.net it is done automatically by framework.

in general, there may be different techniques, such as creating a mutex (some vb example here, but works for wince as well - http://www.mrexcel.com/forum/showthread.php?t=19425), registering your unique window class and then calling FindWindow() to check if there is such one already etc.
0
 

Author Comment

by:uveper
ID: 24770632
what do you mean "with cf.net it is done automatically by framework" ?

I tried to convert vb example to c# but I failed, because I do not fully understand the syntax.
0
 

Accepted Solution

by:
uveper earned 0 total points
ID: 24770808
I found a solution!
using System.Runtime.InteropServices;
 

const string AppNamedEvent = "APP_NAMED__EVENT";
 

        [DllImport("coredll")]

        private extern static void SetWindowText(IntPtr hwnd, string text);
 

        [DllImport("coredll")]

        private static extern IntPtr FindWindow(string className, string wndName);
 

        [DllImport("coredll.dll")]

        private static extern bool SetForegroundWindow(IntPtr hWnd);
 

        [DllImport("coredll.dll", EntryPoint = "CreateEvent", SetLastError = true)]

        private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);
 

        [DllImport("coredll.dll", EntryPoint = "CloseHandle", SetLastError = true)]

        private static extern IntPtr CloseHandle(IntPtr handle);
 

//Before you do the app.run use the following code

                // makes sure that we have only one run instance

                IntPtr evnt = CreateEvent(IntPtr.Zero, false, false, AppNamedEvent);

                if (Marshal.GetLastWin32Error() != 0)

                {

                    // one instance of application is run, activate it

                    IntPtr hwnd = FindWindow(null, "StartForm");

                    if (hwnd != IntPtr.Zero)

                    {

                       SetForegroundWindow(hwnd);

                    }

                    return;

                }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to install QR Code scanner software on LG phone 6 174
Android contacts 1 120
Phones With Stylus Slot 4 78
Heart rate monitor that connect on Samsung Note 4 2 57
This article shows how to setup the internet connectivity on Windows Mobile Emulator.   I assume that you already have Microsoft Visual Studio, Microsoft Windows Mobile SDK's and the emulator installed. The emulator is available with Visual Studi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now