예제 #1
0
        private static void Main(string[] args)
        {
            // Manage unhandled exceptions
            Application.ThreadException += UnhandledThreadException;
            AppDomain.CurrentDomain.UnhandledException += UnhandledException;

            LogVersionStrings();

            int foundTypes = LoadComObjectAssemblies();

            if (foundTypes < 1)
            {
                return; // There is no point continuing if we found nothing to serve.
            }
            if (!ProcessArguments(args))
            {
                return;                          // Register/Unregister
            }
            // Initialize critical member variables.
            objsInUse    = 0;
            serverLocks  = 0;
            MainThreadId = GetCurrentThreadId();
            Thread.CurrentThread.Name = "Main Thread";

            // Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            s_MainForm = new ServerStatusDisplay();

            // #if !DEBUG
            //// Only show the application main window if it was started manually by the user.
            // if (StartedByCOM) s_MainForm.WindowState = FormWindowState.Minimized;
            // #endif

            // Register the class factories of the served objects
            RegisterClassFactories();

            // Start up the garbage collection thread.
            var GarbageCollector = new GarbageCollection(10000);
            var GCThread         = new Thread(GarbageCollector.GCWatch);

            GCThread.Name = "Garbage Collection Thread";
            GCThread.Start();

            // Start the message loop. This serializes incoming calls to our
            // served COM objects, making this act like the VB6 equivalent!
            try
            {
                Application.Run(s_MainForm);
            }
            finally
            {
                // Revoke the class factories immediately.
                // Don't wait until the thread has stopped before
                // we perform revocation!!!
                RevokeClassFactories();

                // Now stop the Garbage Collector thread.
                GarbageCollector.StopThread();
                GarbageCollector.WaitForThreadToStop();
                Application.ThreadException -= UnhandledThreadException;
                AppDomain.CurrentDomain.UnhandledException -= UnhandledException;
            }
        }
예제 #2
0
        private static void Main(string[] args)
        {
            // Manage unhandled exceptions
            Application.ThreadException += UnhandledThreadException;
            AppDomain.CurrentDomain.UnhandledException += UnhandledException;

            var log = CompositionRoot.Kernel.Get <ILog>();

            log.Info()
            .Message("Server start. Version {gitInformationalVersion}", GitVersion.GitInformationalVersion)
            .Property("gitCommitDate", GitVersion.GitCommitDate)
            .Property("gitCommitSha", GitVersion.GitCommitSha)
            .Property("gitSemVer", GitVersion.GitFullSemVer)
            .Write();

            var drivers = CompositionRoot.Kernel.Get <DriverDiscovery>();

            drivers.DiscoverServedClasses();
            if (!drivers.DiscoveredTypes.Any())
            {
                log.Fatal()
                .Message("No driver classes found. Have you added [ServedClassName] attributes?")
                .Property("discovery", drivers)
                .Write();
                return;
            }

            if (!ProcessArguments(drivers, args))
            {
                return;                                   // Register/Unregister
            }
            // Initialize critical member variables.
            objsInUse    = 0;
            serverLocks  = 0;
            MainThreadId = GetCurrentThreadId();
            Thread.CurrentThread.Name = "Main Thread";

            // Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            s_MainForm = new ServerStatusDisplay();

            /*
             * If you do not wish your main form to display when started automatically
             * by a client app, then minimize it to the task bar here.
             */
            //if (StartedByCOM) s_MainForm.WindowState = FormWindowState.Minimized;

            var registeredFactories = RegisterClassFactories(drivers, log);
            // ToDo: [TPL] Why is this even necessary? Shouldn't GC be automatic?
            var garbageCollector = new GarbageCollection(10000);
            var gcThread         = new Thread(garbageCollector.GCWatch);

            gcThread.Name = "Garbage Collection Thread";
            gcThread.Start();

            // Start the message loop. This serializes incoming calls to our
            // served COM objects, making this act like the VB6 equivalent!
            try
            {
                Application.Run(s_MainForm);
            }
            finally
            {
                // Revoke the class factories immediately.
                // Don't wait until the thread has stopped before
                // we perform revocation!!!
                RevokeClassFactories(registeredFactories);

                // Now stop the Garbage Collector thread.
                garbageCollector.StopThread();
                garbageCollector.WaitForThreadToStop();
                Application.ThreadException -= UnhandledThreadException;
                AppDomain.CurrentDomain.UnhandledException -= UnhandledException;
            }
        }