Solved

System.CannotUnloadAppDomainException on form closing

Posted on 2014-09-08
3
489 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…

734 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