2

I need a way to make my app to do this: run by windows running (I did it) then if the user exits the program it hides by this method

        Form1 form = (Form1)sender;
        form.ShowInTaskbar = false;
        form.Location = new Point(10000, 10000);

btw I made a method in the program.cs file by the help of a member that checks if there any instance is running here it is

    [STAThread]
    static void Main()
    {
      

            Mutex mutex = new System.Threading.Mutex(false, "MyUniqueMutexName");
            try
            {
                if (mutex.WaitOne(0, false))
                {
                    // Run the application
                   
                }
                else
                {
                

                    MessageBox.Show("there is already a running instance of this program", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
               

            }
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.Close();
                    mutex = null;
                }
            }
    }

what I want from my program is like the following: I don't want the program to open another instance while there is already an open one I need to find a way to make the program to just make the hidden one to be visible

btw English isn't my mother language so please don't complain last thing I want to tell you guys that I am a 13-year-old programmer

Ibrahim
  • 21
  • 3
  • 1
    Welcome to stackoverflow! Great to see a new developer starting at your age. Please edit your question so that the title reflects the question you are asking like "How to prevent opening second instance of running program". PS - your English is just fine :) – Luke Oct 11 '22 at 17:42
  • 1
    ok I will focus on this point next time + thanks bro <3 – Ibrahim Oct 11 '22 at 17:43
  • 1
    The way this is usually done is by sending a message from the instance of the program that is opening to the instance that is already open. With this approach you need to be able to communicate between instances. [Here](https://stackoverflow.com/a/11359289/10601203) is an answer describing the most popular ways of communicating with another application in Windows. After you've established that, your running instance can listen for the message and focus itself when it receives it, then your second instance can send the message before it closes itself. – Jesse Oct 11 '22 at 17:49
  • 1
    Alternatively you can use window handles with the Win32 API to move/focus the window from the program opening, but this gives you a lot less control than if you were to handle the message within the open program itself. – Jesse Oct 11 '22 at 17:52
  • 1
    There's no need to wait until next time - please use the "Edit" link at the bottom of the question and update the title. – Jon Skeet Oct 11 '22 at 17:56
  • oh wow thanks bro i didnt notice that button @JonSkeet – Ibrahim Oct 11 '22 at 18:03

0 Answers0