private void fmNexus_Load()

in sqlnexus/fmNexus.cs [381:539]


        private void fmNexus_Load(object sender, EventArgs e)
        {
            

            // Write our location to the registry so ReadTrace.exe can find us. 
            Registry.SetValue(REG_HKCU_APP_KEY, "InstallPath", 
                Path.GetDirectoryName(Application.ExecutablePath));
            Registry.SetValue(REG_HKCU_APP_KEY2, "InstallPath",
                Path.GetDirectoryName(Application.ExecutablePath)); 

            //Undo weird effect where Windows gets confused because
            //we're not flagged a GUI app
            // TODO: have console detect whether it's minimized and pass this info to fmNexus so that 
            // fmNexus can preseve a minimized state (e.g. START /MIN sqlnexus.exe ...). Low priority.
            this.WindowState = FormWindowState.Minimized;
            Application.DoEvents();
            if (!Globals.NoWindow && !Globals.ConsoleMode)
                this.WindowState = FormWindowState.Maximized;

            MakeTaskPaneImagesTransparent();

            SetupSpinners();

            AutoHide (false);

            Application.DoEvents();

            //Nuke image cache
            if (0 != Directory.GetDirectories(Path.GetTempPath(), "sqlnexus").Length)
                Directory.Delete(Path.GetTempPath() + "sqlnexus", true);

            // InitializeLog(Application.StartupPath + @"\sqlnexus.log");
            String logFileFullPath = String.Empty;
            if (Globals.ReportExportPath != null)
            { 
                logFileFullPath = Globals.ReportExportPath + @"\sqlnexus.log";
                if (!Directory.Exists (Globals.ReportExportPath ))
                {
                    Directory.CreateDirectory (Globals.ReportExportPath);
                }
            }
            else
            {
                logFileFullPath = @"%TEMP%\sqlnexus.log";
            }
            //FileMgr mgr = new FileMgr();
            //MessageBox.Show (mgr.ToString());
            RuntimeEnv.Env.NexusLogFile =logFileFullPath;
            InitializeLog(logFileFullPath);
            LogMessage("sqlnexus.exe running at: " + Application.ExecutablePath + " version " + Application.ProductVersion);
//#if BETA
//            LogMessage("This is a beta version");
//#endif 
            InitAppEnvironment();
           
            // Kick off async (background) ClickOnce autoupdate check.  Results of check will be 
            // written to the log file
            clickOnce.TraceLogger = this.TraceLogger;
            clickOnce.UpdateApplicationAsync();
            
          
            if (!Globals.ConsoleMode)
            {

                if (DialogResult.OK != ShowConnectionDlg())
                {   // User clicked Cancel on connection dialog
                    Application.Exit();
                    return;
                }
                if (Util.GetReadTracePath() == null)
                {
                    string dlgTitle = "Cannot Locate ReadTrace Path";
                    LogMessage("Unable to locate readtrace.  Nexus won't be able to load or analyze profiler trace data.", MessageOptions.All, TraceEventType.Error, dlgTitle);
                }
                else if (File.Exists(Application.StartupPath + @"\readtracenexusimporter.dll") == true)
                {
                    LogMessage("Extracing ReadTrace reports", MessageOptions.Silent);
                    Assembly assem = Assembly.LoadFile(Application.StartupPath + @"\readtracenexusimporter.dll");
                    INexusImporter ri = (INexusImporter)assem.CreateInstance("ReadTrace.ReadTraceNexusImporter", true);
                    ri.Initialize("*.out", Globals.credentialMgr.ConnectionString, Globals.credentialMgr.Server, true, "", "", Globals.credentialMgr.Database, this);
                }
                else
                {
                    LogMessage("Found readtrace path but ReadtraceNexusimporter.dll is not Available");
                }
                if (Util.GetReadTracePath() != null && !File.Exists(Util.GetReadTraceExe()))
                {
                    string dlgTitle = "Cannot Find ReadTrace.exe";
                    LogMessage("SQL Nexus located readtrace path as " + Util.GetReadTracePath() + " but it can't locate Readtrace.exe in this directory.\n\rYou have incorrect installation.  Trace import won't work", MessageOptions.All, TraceEventType.Error, dlgTitle);
                }

                
                
                
                string inst=Globals.credentialMgr.Server;

                int i=inst.IndexOf('\\');
                if (-1 != i)
                {
                    inst = inst.Substring(i + 1);
                }
                else
                {
                    inst = "(Default)";
                }

                UpdateSQLDiagInstance(inst);

                if (false)
                {
                 //   PromptStartCollector();
                }
                    
            }
            Cursor save = StartWaiting();
            try
            {
                try
                {
                    EnumReports();
////                    InitCollectorService();

                    //if running in console mode (command line) , call ProcessReportQueue()
                    if (Globals.ConsoleMode)
                    {
                        //if it didn't process reports (due to db already present and contains nexus data) quit
                        if (false == ProcessReportQueue())
                        {
                            Thread.Sleep(500);
                            Application.Exit();
                        }

                        // If we were passed /X, exit after importing data and exporting reports
                        if (Globals.ExitAfterProcessingReports)
                            Application.Exit();
                    }
                    else
                    {
                        //Select the first report if there is one
                        if (0 != tvReports.Nodes.Count)
                            tvReports.SelectedNode = tvReports.Nodes[0];

                        ShowHideUIElements();

                        Application.DoEvents();

////                        UpdateServiceButtons();
                    }
                }
                catch (Exception ex)
                {
                    Globals.HandleException(ex, this, this);
                }
            }
            finally
            {
                StopWaiting(save);
            }
        }