Solved

System.CannotUnloadAppDomainException on form closing

Posted on 2014-09-08
3
430 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

19 Experts available now in Live!

Get 1:1 Help Now