none
catch AppDomain.CurrentDomain.UnhandledException,but can't cancel terminate CLR RRS feed

  • Question

  •     I am programming a "console Application" software that based on add-ins. My idea is MainApp includes more AppDomains,each loads one or more  assemblys. If required, I can manage  special assemly in unloading ,replace old and add new one.

        Now I have created AppDomain  and assembly,   I have also implemented assembly load. But AppDomain.CurrentDomain.UnhandledException is a most question for me.

        My software maybe includes our components and third part components. So I hope I am able to catch AppDomain.CurrentDomain.UnhandledException. If third part component raise some exception. I can catch it and solve it.

      I have catched UnhandledException ,but UnhandledExceptionEventArgs.IsTerminating always to be True, if exception raised, my application will be down. Could you help me to cancel terminate CLR if I catch UnhandledException?

      This is mY codes.

      public class Tank

        {
       
         public Tank()
            {
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
               
            }

            void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                 // I don't know how to cancel terminate CLR here          

            }

           
            public void Move(int x,int y)
            {
                Int32 nThreadID = Thread.CurrentThread.ManagedThreadId;
                Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
                AppDomain newAppDomain = AppDomain.CreateDomain("try_Addin");
                try
                {
                    TankAddin instTankAddin = newAppDomain.CreateInstanceAndUnwrap("TADEAddin",  "TADEAddin.TankAddin") as TankAddin;
                    instTankAddin.Run();
                }
                catch (System.Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.Read();
                AppDomain.Unload(newAppDomain);

            }

         }
    }

     

     

    namespace TADEAddin
    {
        public class TankAddin : MarshalByRefObject
        {
            public void Run()
            {
                Thread instThread = new Thread(MyThread);
                instThread.Start(this);
            }

            public static void MyThread(Object obj)
            {
                TankAddin addin = (TankAddin)obj;
                addin.Move(0, 0);
            }

            public void Move(int x, int y)
            {
                Int32 nThreadID = Thread.CurrentThread.ManagedThreadId;
                Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
                if (x == y && x == 0)
                {
                    throw new Exception("addin param failed");   // exception raised .
                }
            }
        }
    }

    Sunday, May 4, 2008 11:16 AM

Answers

  • You can enable this behviour without the need to host the CLR. The details are already covered in this post.

    MSDN Link

    Yours,
      Alois Kraus

    Sunday, May 4, 2008 11:30 PM
  • A lot of work was done in version 2.0 of the CLR to support what you are looking for.  Most of all to allow SQL Server to host managed code.  However, this support is only available to you if you write your own CLR host.  And even then only in a carefully controlled execution environment, one where a thread can be terminated without destabilizing the app.

    Doing this is not for the faint of heart.  I would recommend you start with Steven Pratschner's book to see what is needed. 

    A possibly more pragmatic approach is one where you load the plug-ins in a separate helper process, one that can terminate without bringing your own app down.  You'd use .NET Remoting and an IpcChannel to talk to it.
    Sunday, May 4, 2008 3:19 PM
    Moderator

All replies

  • A lot of work was done in version 2.0 of the CLR to support what you are looking for.  Most of all to allow SQL Server to host managed code.  However, this support is only available to you if you write your own CLR host.  And even then only in a carefully controlled execution environment, one where a thread can be terminated without destabilizing the app.

    Doing this is not for the faint of heart.  I would recommend you start with Steven Pratschner's book to see what is needed. 

    A possibly more pragmatic approach is one where you load the plug-ins in a separate helper process, one that can terminate without bringing your own app down.  You'd use .NET Remoting and an IpcChannel to talk to it.
    Sunday, May 4, 2008 3:19 PM
    Moderator
  • You can enable this behviour without the need to host the CLR. The details are already covered in this post.

    MSDN Link

    Yours,
      Alois Kraus

    Sunday, May 4, 2008 11:30 PM