Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Prevent a .NET program for starting if the .exe is in us
Message
From
26/03/2017 17:04:36
 
 
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01649285
Message ID:
01649348
Views:
37
>>>>Can that be done?
>>>>How?
>>>
>>>Click on "Project\[Project] properties\Application\Windows application framework properties\Make single instance application".
>>
>>Thank you, Michel.
>>I'll check that out
>
>I think that's a VB only option. The standard way is to use a mutex. Example here (with a few options on how to handle attempts to open a second instance):
>http://sanity-free.org/143/csharp_dotnet_single_instance_application.html

Thank you for link, Viv
After looking at several links and borrowing freely, here's what I wound up with
It's working well.
            static void Main()
        {

            // get application GUID as defined in AssemblyInfo.cs
            string appGuid =
                ((GuidAttribute)Assembly.GetExecutingAssembly().
                    GetCustomAttributes(typeof(GuidAttribute), false).
                        GetValue(0)).Value.ToString();

            // unique id for global mutex - Global prefix means it is global to the machine
            string mutexId = string.Format("Global\\{{{0}}}", appGuid);

            // Need a place to store a return value in Mutex() constructor call
            bool createdNew;

            var allowEveryoneRule =
                new MutexAccessRule(
                    new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                    MutexRights.FullControl, AccessControlType.Allow);
            var securitySettings = new MutexSecurity();
            securitySettings.AddAccessRule(allowEveryoneRule);

            using (var mutex = new Mutex(false, mutexId, out createdNew, securitySettings))
            {
                var hasHandle = false;
                try
                {
                    try
                    {
                        hasHandle = mutex.WaitOne(5000, false);
                        if (hasHandle == false)
                        {
                            throw new System.TimeoutException("Timeout waiting for exclusive access");
                        }
                    }
                    catch (AbandonedMutexException)
                    {
                        // Log the fact that the mutex was abandoned in another process,
                        // it will still get acquired
                        hasHandle = true;
                    }

                    // Perform your work here.
                    SQLServerMethods sqlServerMethods = new SQLServerMethods();
                    sqlServerMethods.StartApp();


                }

                catch (System.TimeoutException)
                {
                    // Log the fact that the mutex was abandoned in another process,
                    // it will still get acquired
                    MessageBox.Show("This application is limited to one user at a time. Try again later.");

                }
                finally
                {
                    if (hasHandle)
                        mutex.ReleaseMutex();
                }

            }
        }
Anyone who does not go overboard- deserves to.
Malcolm Forbes, Sr.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform