Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 552
  • Last Modified:

System.CannotUnloadAppDomainException on form closing

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
fantamen
Asked:
fantamen
  • 2
1 Solution
 
iHadiCommented:
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
 
fantamenAuthor Commented:
0
 
fantamenAuthor Commented:
The merit is not mine. I've only find the solution in a expert Exchange discussion.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now