コード例 #1
0
        static void Main(string[] args)
        {
            Cursor.Current = Cursors.WaitCursor;
            //Application.EnableVisualStyles();  // Debug: Need to investigate this more before using

            // Try to load the assorted SysInfo in from disk.  If there's no file (first time ever or it was deleted) then initialize with default info
            InitializeSysInfo();           // Now we can set the rest of the reference data variables

            // Initialize Reference data such as geographical info, mobile devices, and languages
            //InitializeRefData();  // Debug: To Do!

            Tools.HandleTempDirectory(SysInfo.Data.Paths.Temp, true); // Ensures Temp folder exists and all possible files are removed

            // Check if any command line arguments were passed to the executable.  Among other things...
            // Find out if the "DataTransfer" parameter was passed to the EXE upon startup.
            // If so, then 'SysInfo.Data.Admin.DataTransferActive' will be set true.
            HandleStartupParameters(args);

            if (Uninstall)
            {
                return; // Quietly exit
            }
            else if (!SysInfo.Data.Admin.DataTransferActive)
            {
                if (SysInfo.Data.Options.ShowSplash)
                {
                    frmSplash.ShowSplashScreen();
                    Application.DoEvents();
                }
            }

            // Now see whether a copy of the app is already running.
            Process aProcess  = Process.GetCurrentProcess();
            string  aProcName = aProcess.ProcessName;

            // Determine how we're going to proceed
            if (Process.GetProcessesByName(aProcName).Length == 1) // If true, then this is the first copy of the executable to be started
            {
                // If this executable is running then we DON'T want another copy started by ActiveSync
                RegistryTools.DeleteAutoStartOnConnect();

                // This should always be set to true so that the partnership dialog box doesn't appear
                RegistryTools.SetGuestOnly(true);

                Application.ApplicationExit += new EventHandler(ShutDownApp); // Method that'll be called when shutting down

                // Instantiate the poll manager
                _current = new PollManager();

                // Instantiate an instance of frmMain
                FormMain = new frmMain();
                FormMain.SetMenuItems(0);

                // This imports any residual data files that failed to get imported the last time the app was run.  Generally the only
                // time such files would exist would be if the app previously crashed.
                FormMain.ImportNewDataFiles();

                InitializeCopyData();

                TestCode(); // Only used during debugging; normally empty

                bool okayToStartupApp = true;

                // Check whether the app is being run for the very first time ever.
                if (SysInfo.Data.Options.PrimaryUser == "")
                {
                    FormMain.Show(); // Doing this simply to provide standard visual background (behind Options form)

                    // We'll startup the Options form in a special way that will force (strongly request) a UserName be specified
                    frmOptions optionsForm = new frmOptions(true);

                    // If user really, really didn't want to specify a username then we MUST exit app
                    if (SysInfo.Data.Options.PrimaryUser == "")
                    {
                        okayToStartupApp = false;
                        FormMain.ExitApp();
                    }
                }

                // Now check that the file association(s) are properly setup for this app
                CheckFileAssociations();

                if (okayToStartupApp)
                {
                    if (Tools.IsAppExpired())
                    {
                        FormMain.Freeze();
                    }
                    else
                    {
                        // "StartupFilename" is set in HandleStartupParameters
                        if (StartupFilename != null)
                        {
                            FormMain.PreparePoll(StartupFilename);
                        }
                    }

                    Cursor.Current = Cursors.Default;
                    Application.Run(FormMain);
                }
            }

            else // A copy of the app is already running
            {
                if (SysInfo.Data.Admin.DataTransferActive)
                {
                    // Since the data transfer thread will already have been started by the RAPI.ActiveSync.Active event
                    // we don't need to start a 2nd copy of this application.  So just exit quietly.
                    // Note: Because we now remove the AutoStartOnConnect string, we'll actually never get to here
                    //       but it doesn't hurt to keep this extra code regardless.
                    Application.ExitThread();
                }
                else
                {
                    // if another process exists, send file name as message and quit
                    if (StartupFilename != null)
                    {
                        // Create a new instance of the class
                        CopyData copyData = new CopyData();

                        // Create the named channels to send and receive on.  The name is arbitrary; it can be anything you want.
                        copyData.Channels.Add(SysInfo.Data.Admin.AppName);

                        // Send filename
                        copyData.Channels[SysInfo.Data.Admin.AppName].Send(StartupFilename);
                    }
                    else // 2nd copy started without any specific file so don't allow it
                    {
                        // Prevent 2nd copy of executable from running
                        Tools.ShowMessage("The " + SysInfo.Data.Admin.AppName + " Desktop application is already running!", SysInfo.Data.Admin.AppName);
                        Application.ExitThread();
                    }
                }
            }
        } // End of "Main"