Creating Single Insatance Application in SmartDevice Application

Hai,

           I have created DeviceApplication present under Visual C#\SmartDevice\Windows Mobile 5.0 Pocket PC.The out of this is an exe.I want to make this exe to have single insatnce i.e if one instance is running,if u click on that exe for multiple times it must not new instances,instead it must pop-up some message.I have developed the exe in .NET 2.0 version.I tried to do fix the problem using mutex,but it did not work.Another approach is to derive a class from the WindowsFormsApplicationBase class in the Microsoft.VisualBasic.ApplicationServices namespace and set IsSingleInstance property.Can anyone explain how to do this for Smart Device Application.I found that GetProcessByName is not supported for SmartDevice Application.

Thanks in Advance,

Sunil

 

 

 

 

 



Answer this question

Creating Single Insatance Application in SmartDevice Application

  • robear

  • Alle

    Thanks Michael -- it worked for me also..

    Can i know how it is done i.e i want understand more how it is handled.Any artical or links will help me...

    Thanks anyways...

    Sunil


  • pauloschultz

    Hi Rob

    This code works fine for me:

    sing System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Text;

    namespace SingleInstanceTest
    {
    static class Program
    {
    [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;
    [DllImport("coredll.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string className, string wndName);


    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [MTAThread]
    static void Main(string[] commandLineArg)
    {
    string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
    IntPtr handle = CreateEvent(IntPtr.Zero, false, false, path);
    int error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
    if (handle != null && (error != ERROR_ALREADY_EXISTS))
    {

    Application.Run (new Form1 (commandLineArg));
    }
    else
    {
    // Upon startup CF implementation of mscoree looks for a window with a class #NETCF_AGL_PARK
    // and title set to the current process executable full path (\Program Files\MyApp\MyApp.exe).
    // If found, the runtime presumes that the current application is already running. In this case
    // it will be reactivated. To do this the abovementioned window is sent a message 0x8001,
    // and then the new instance quits.

    // The CF 2.0 is almost identical: This is only when you start the app multiple times before the Form has
    // been appeared.
    IntPtr hWnd = FindWindow("#NETCF_AGL_PARK_"+path, string.Empty);
    if (hWnd != IntPtr.Zero)
    {
    Microsoft.WindowsCE.Forms.Message msg = Microsoft.WindowsCE.Forms.Message.Create(hWnd, (int)0x8001, (IntPtr)0, (IntPtr)0);
    Microsoft.WindowsCE.Forms.MessageWindow.SendMessage(ref msg);
    }
    }
    CloseHandle (handle);
    }
    }
    }

    Michael



  • kbromer

    You don't need to do anything, that's default behavior on WM.


  • McVicar

    Hi i have almost the same problem
    if i close my application it seems as it is closed
    i can't se it under running programs and i can see that some memory is released

    but if i use Windows CE Remote Proccess Viewer i can see that my app is still running
    and if i start my app again by hitting the exe
    there comes another process in remote process viewer

    i have tried to use your above code example to ensure that there is only one instance
    but no luck

    i am using .NET CF 2.0

    i have noticed that on CF 2.0 & ppc 2003 the whe window name when the app is minimized is FAKEIMEUI

    and when i try to close my programm and it only shows up in remote process viewer the window has no name

    any suggjestions


    ps. i am using a couple of threads in my app and am almost 100% shure that they are beeing aborted when i close my app

    pps. am also using a couple of timers

  • bxs122

  • cgwaters

    I am having the exact same problems on Windows Mobile 5.0 smartphone. Using a Mutex did not solve the problem.

    I have a homescreen plugin that uses CreateProcess() to launch my application. I can click on this several times really fast (ENTER on it using DPAD) and it will launch many instances of my application, even though in my program's main(), I am creating a Mutex and failing out if it already exists.

    I read this thread:

    http://groups.google.com/group/microsoft.public.dotnet.framework.compactframework/browse_thread/thread/e8352589e189e29a/56673cd042e7578a lnk=st&rnum=1#56673cd042e7578a

    It didn't really shed any light on the issue. The default multiple instance checking behavior built into the .NET Compact Framework seems to be totally broken.

    I should be able to check for previous instances of the app in my main, and if already running, activate previous instance and then exit silently--just as a native C++ Windows CE application can do pretty easily.

    Does anyone have a real solution that works 100% of the time

    Rob


  • &amp;#64;nt

    Whenever i debug it first hits "Application.Run(new DCTForm());" itself later i am doing it will call InitializeComponent();and then i am creating the log file by using File.CreateText(mFileName);I am not using any load event.lease give some inputs..


  • Jason D. Camp

    Hai Ilya,

    Initially when my exe is pressed i am creating a file using File.CreateText to log information about my application.It is basically a log file.The problem i am facing is when i press the exe multiple times,that file is being created again and again,so it pop-up a message saying "An unexpected error has occured in DeviceConfigTool.exe.Select Quit and restart this program,or select details for more information.The process can not access the file '\ProgramFiles\DeviceConfigTool\Log.Txt' because it is being used by another process.I want to prevent the creation of that file each time i click on the exe.how to create the file only when another insatance of the exe is not running



  • Drew Marsh

    Please try it with empty application.



  • paul holmes

    Thanks Michael--that approach worked well. Many thanks!

    Rob


  • Tom_Liu

    See of moving your file creation code to Load event helps.



  • AKA13_Intrepid

    Don't create any files before your main form is created. At this point single instance logic would engage and your second copy would be terminated while first one would be maximized.

    To do so make sure the first executed command is Application.Run() with your form, do nothing prior to that. Process log files after that, say from Form.Load event.



  • Alexnaldo Santos

    I tried to create the file in load event but it did not help.
  • Creating Single Insatance Application in SmartDevice Application