Example #1
0
        static void Main()
        {
            bool newInstance;

            using (Mutex mutex = new Mutex(false, "VCover", out newInstance))
            {
                if (newInstance)
                {
                    try
                    {
                        ThreadPool.SetMaxThreads(50, 100);
                        Thread.CurrentThread.CurrentCulture       =
                            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-us");

                        StartUp.TryToAddAppSafe();

                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);

                        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                        Application.ThreadException += new ThreadExceptionEventHandler(OnThreadException);

                        var ctn = new AppContext();
                        ctn.NewInFileEvent += new EventHandler <ValueEventArgs <Guid, string> >(NewCommandFileEvent);
                        ctn.Started        += new EventHandler(ctn_Started);

                        StateSaver.Error         += new ThreadExceptionEventHandler(OnThreadException);
                        AppContext.Default.Error += new ThreadExceptionEventHandler(OnThreadException);

                        StateSaver.Default.Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "VCover.dat");
                        StateSaver.Default.Load();

                        TemplateMatcher.Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TemplateMatcher.dat");
                        TemplateMatcher.Load();

                        NamedPipes.ReceivedData += new ReceivedDataDelegate(NamedPipes_ReceivedData);
                        NamedPipes.Error        += new ThreadExceptionEventHandler(OnThreadException);
                        NamedPipes.StartServer("VCOVER");
                        Application.Run(ctn);
                    }
                    finally
                    {
                        StateSaver.Default.Save();
                        TemplateMatcher.Save();
                    }
                }
                else
                {
                    MessageBox.Show("An instance is already started.\r\nPlease close it first.",
                                    Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }