Solved

System.CannotUnloadAppDomainException on form closing

Posted on 2014-09-08
3
445 Views
Last Modified: 2014-09-13
In Program.cs of my c# Windows form application I've write this Main void:

static class Program
    {
        static Mutex cvAppMutex = new Mutex(true, "Spv_CheckAdvance");
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                // Faccio in modo che il thread principale acquisisca un mutex in modo tale che se avvio più di una
                // volta l'applicazione il secondo thread viene respinto. In questo modo impedisco più di un avvio
                // dell'applicazione.
                // bool lvFirst = false;
                //cvAppMutex = new Mutex(true, "Spv_CheckAdvance", out lvFirst);
                if (cvAppMutex.WaitOne(TimeSpan.Zero, true))
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MenuPrincipale());
                    cvAppMutex.ReleaseMutex();
                }
                else
                {
                    MessageBox.Show("Applicazione già in esecuzione!", "Spv_CheckAdvance:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message, "Main()", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

Only when I open a particular form from my menù when I close the MenuPrincipale form this error appear:
Message: An unhandled exception of type 'System.CannotUnloadAppDomainException' occurred in mscorlib.dll
Additional information: Errore durante lo scaricamento del dominio applicazione. (Eccezione da HRESULT: 0x80131015)

Every object are disposed before closing. I don't know what I have to check again.
0
Comment
Question by:fantamen
  • 2
3 Comments
 
LVL 13

Expert Comment

by:iHadi
ID: 40309753
Hi Fantamen,

Try the following code instead of your current one:

    static class Program
    {
        static Mutex cvAppMutex;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool mutexNew = false;

            cvAppMutex = new Mutex(true, "Spv_CheckAdvance", out mutexNew);
            if (!mutexNew)
            {
                MessageBox.Show("Applicazione già in esecuzione!", "Spv_CheckAdvance:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            cvAppMutex.ReleaseMutex();
        }
    }

Open in new window

0
 

Accepted Solution

by:
fantamen earned 0 total points
ID: 40309776
0
 

Author Closing Comment

by:fantamen
ID: 40320690
The merit is not mine. I've only find the solution in a expert Exchange discussion.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now