Running a single instance of an app

V

VicToro

Hi,

I am running a background process (Console App) on a WM5 Pocket PC.
I'd like to have just one instance of the process running at the same
time. Something as giving the main method the property to be Static
Thread or looking if the Process is unique.

Since STAThread directive is not used (whereas MTAThread is used) and
the class System.Diagnostic.Process does not have the method
GetProcesses, I cannot find the way to keep the process unique on the
device.

Any suggestion?

Cheers,
Victor
 
U

usunto_bryjamus

Try this:


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

[DllImport("coredll.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);

private const int ERROR_ALREADY_EXISTS = 183;

/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main()
{
IntPtr handle = CreateEvent(IntPtr.Zero, false, false,
Assembly.GetExecutingAssembly().GetName().CodeBase);

int error = Marshal.GetLastWin32Error();

if (handle != null && (error != ERROR_ALREADY_EXISTS))
{
Application.Run(new MainWindowClass());
}
else
{
IntPtr windowHandle = FindWindow("#NETCF_AGL_PARK_" +
Assembly.GetExecutingAssembly().GetName().CodeBase, string.Empty);

if (windowHandle != IntPtr.Zero)
{
Message msg = Message.Create(windowHandle, (int)0x8001,
(IntPtr)0, (IntPtr)0);
MessageWindow.SendMessage(ref msg);
}
}
CloseHandle(handle);
}
 
G

Guest

This isn't the best mechanism to activate an existing instance. You should
use a thread dedicated to listening to an "activate me" type event. The
reason is that FindWindow iterated through all windows sending them a
WM_GETTEXT via the blocking SendMessage call. If any app window before
yours is in a wait state then the call to SendMEssage, and therefore
FindWindow, will block. This could deadlock your application.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com



Try this:


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

[DllImport("coredll.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);

private const int ERROR_ALREADY_EXISTS = 183;

/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main()
{
IntPtr handle = CreateEvent(IntPtr.Zero, false, false,
Assembly.GetExecutingAssembly().GetName().CodeBase);

int error = Marshal.GetLastWin32Error();

if (handle != null && (error != ERROR_ALREADY_EXISTS))
{
Application.Run(new MainWindowClass());
}
else
{
IntPtr windowHandle = FindWindow("#NETCF_AGL_PARK_" +
Assembly.GetExecutingAssembly().GetName().CodeBase, string.Empty);

if (windowHandle != IntPtr.Zero)
{
Message msg = Message.Create(windowHandle, (int)0x8001,
(IntPtr)0, (IntPtr)0);
MessageWindow.SendMessage(ref msg);
}
}
CloseHandle(handle);
}


Hi,

I am running a background process (Console App) on a WM5 Pocket PC.
I'd like to have just one instance of the process running at the same
time. Something as giving the main method the property to be Static
Thread or looking if the Process is unique.

Since STAThread directive is not used (whereas MTAThread is used) and
the class System.Diagnostic.Process does not have the method
GetProcesses, I cannot find the way to keep the process unique on the
device.

Any suggestion?

Cheers,
Victor
 

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