Show form in static main

johnlee

New member
Joined
Feb 11, 2019
Messages
3
Programming Experience
3-5
Hi everybody.
I have 2 form : MainFrm is main form and Notification is form used to show notification.
In program.cs :
C#:
static void Main()
        {
            bool result;
            Mutex mutex = new Mutex(true, "UniqueAppId", out result);


            if (!result)
            {
                //MainFrm.showNotification("KyAppAPI??????????");


                //MessageBox.Show("KyAppAPI??????????");


                Notification notiBox = new Notification();
                notiBox.Show();
                return;
            }


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainFrm());


            GC.KeepAlive(mutex);
        }

I use mutex to keep application run once. If application already running, form Notification will show.
But my code work not right, form Notification not shown.
In MainFrm, I has a static method showNotification (using Tulpep Notification). I try calling this method in static main, but it not working, too.
However, if I use MessageBox, it working fine.
I don't know where the problem is. Please help me.
 
The Show method doesn't block, so your application is exiting before you get to see the form. You should be calling ShowDialog, which will not return until the form is closed.

By the way, you should be calling EnableVisualStyles and SetCompatibleTextRenderingDefault before showing either form.
 
Last edited:
The Show method doesn't block, so your application is exiting before you get to see the form. You should be calling ShowDialog, which will not return until the form is closed.

By the way, you should be calling EnableVisualStyles and SetCompatibleTextRenderingDefault before showing either form.

OK, I resolved it, thank you so much !
 
@jmcilhinney : My notification is shown, but I want it only show once on screen. My code :
C#:
static void Main()
        {
            bool result;
            Mutex mutex = new Mutex(true, "UniqueAppId", out result);


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            if (!result)
            {
                //MessageBox.Show("count = " + Application.OpenForms.Count); 
                if (Application.OpenForms["Notification "] == null)
                {
                    Notification notiBox = new Notification();
                    notiBox.show(); 
                }
                return;
            }


            
            Application.Run(new MainFrm());


            GC.KeepAlive(mutex);
        }

if (Application.OpenForms["Notification "] == null) , this conditional always right, so Notification form always create new.
I use message box print Application.OpenForms.Count and it always return 0. Why that?
 
Why would it be more than zero if you haven't opened a form yet? Remember that each time you execute this Main method it is in a different instance of your application, not the same one every time. Every time you enter that method, there are no open forms in the current application. If you want to be able to track something across application instances then you need to use something that all application instances can access.
 
Back
Top Bottom