0

Basically I have C# winforms application with single instance openable on a system using below lines of code.

internal static class Program
{
    private static readonly Mutex _mutex = new Mutex(true, "f70bd07d-e882-469c-8bcd-d2a267ab0602");

    [STAThread]
    private static void Main()
    {
        if (_mutex.WaitOne(TimeSpan.FromSeconds(3), true))
        {
            Application.Run(MyForm);
            _mutex.ReleaseMutex();
        }
        else
        {
            MessageBox.Show("Sorry, another user is currently using.",
                "Information",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button1);
        }
    }
}

So, in above case I want to show a user details who have opened my application initially to the second user who attempt to open it.

I have tried to add some static class property, but it is not storing value from first user.

Expected results should be

internal static class Program
{
    private static readonly Mutex _mutex = new Mutex(true, "f70bd07d-e882-469c-8bcd-d2a267ab0602");

    [STAThread]
    private static void Main()
    {
        if (_mutex.WaitOne(TimeSpan.FromSeconds(3), true))
        {
            Application.Run(MyForm);
            _mutex.ReleaseMutex();
        }
        else
        {
            MessageBox.Show("Sorry, another user " + userName + " is currently using.",
                "Information",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button1);
        }
    }
}
CSharpDev
  • 370
  • 1
  • 7
  • 28
  • write it in a shared memory or registry or file – Code Name Jack Apr 04 '19 at 14:06
  • either you use a storage ( file, db-table, etc) where you store this information temporarily and read from it if necessary, or you try to get the handle of the running application and pull the data from it. You can guess which version is easier to implement ;) – Mong Zhu Apr 04 '19 at 14:07
  • Be carefull with writing this information into a file, or table or registry. What happens if your application crashes or for some other reason the information does not gets cleared when closing your application. Then nobody can start it anymore – GuidoG Apr 04 '19 at 14:10
  • If your application connects to a sql-server, you could retrieve the sessions from the database and check if your application has another session open – GuidoG Apr 04 '19 at 14:16
  • @GuidoG, if I write something to sql-server then their I have no control over application close(window cross button) on which I do not get event to erase this information from db. – CSharpDev Apr 04 '19 at 14:27
  • No, NOT write anything to sql-server. When your application starts, just retrieve all active sessions from your sql-server and check if there still is an active session with another instance of your application. There is no need to write anything to sql-server. This method means you dont have to write anything anywhere – GuidoG Apr 04 '19 at 14:47
  • "What happens if your application crashes .... Then nobody can start it anymore" that is not necessarily true, you would only save the `userName` there. The `_mutex` takes care whether the application ca be started or not. If not then load the data and display it. – Mong Zhu Apr 04 '19 at 14:52

0 Answers0