Solved

System.CannotUnloadAppDomainException on form closing

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

825 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