C#Windows窗体永远需要加载

时间:2013-04-30 15:46:35

标签: c# winforms performance startup

我正在开发一个Windows窗体启动器/更新/启动画面应用程序,用于C#中的Windows 7 Panasonic Toughbook(Tablet)。启动器在我的桌面和我的一本Toughbooks上工作正常......但是,在“真实环境”设备上进行测试需要20多秒才能在第一次启动后显示表单。 (一旦显示表单,它就会快速运行,就像应用程序的工作方式一样。)安装后首次在设备上启动很快,但在首次启动后,需要很长时间。

我不确定如何进行测试,因为这是在无法通过USB连接或放在我们的网络上的设备上。应用程序是否打开文件似乎并不重要。事实上,在大多数情况下,应用程序只是打开,看到现在检查另一个更新,启动主应用程序并自行杀死为时尚早。

目前的流程如下:

构造

public MyConstructor() { InitializeComponent(); }

表单加载:

private void Form1_Load(object sender, EventArgs e)
{
    if (!isLoaded)
    {
        System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap("parentApplication.exe.config"); //Path to your config file
        System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
        log4net.Config.XmlConfigurator.Configure();
        WebRequest.url = configManager.AppSettings.Settings["SURL"].Value;

        // Set a timer to run the update process momentarily to allow the UI thread to update and display. (UI must be idle to run Timer)
        updateTimer = new System.Timers.Timer(1000);
        updateTimer.Elapsed += new ElapsedEventHandler(beginUpdate);
        updateTimer.Enabled = true;
        isLoaded = true;
    }
}

更新流程

private void beginUpdate(Object sender, EventArgs e)
{
    // If any of this fails, we still want to launch the main application. 
    try
    {
            // Prevent the timer from doing anything else.
            updateTimer.Enabled = false;

            string version = "0";
            try
            {
                Version v = AssemblyName.GetAssemblyName("ParentApplication.exe").Version;
                version = v.ToString();
            }
            catch
            {
                version = "0";
            }

            updateProgress(5);

            DateTime buffer = DateTime.Now.AddMinutes(-5);
            DateTime last = Convert.ToDateTime(configManager.AppSettings.Settings[lastCheck].Value);
            int comp = DateTime.Compare(buffer, last);

            if (comp < 0)
            {
                // Begin update process
                updateApplication(version);
            }

            updateProgress(100);

            System.Threading.Thread.Sleep(1000);
        }
        catch (Exception er)
        {
            logger.Error("Error in update application.", er);
        }

        // The updater can't update itself. Launch the external application
        // to handle the updating of the updater. This application also launches
        // the main executable.
        Process sync = new Process();
        sync.StartInfo.UseShellExecute = false;
        sync.StartInfo.FileName = "FinishUpdate.exe";
        sync.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        sync.StartInfo.Verb = "runas";
        sync.Start();
        Application.Exit();
}

1 个答案:

答案 0 :(得分:1)

根据描述,可能难以设置分析器。另一种选择是运行StopWatch,将方法执行时间记录到任意日志文件中。一旦确定哪种方法性能不佳,就可以重复这些步骤,直到缩小导致瓶颈的代码行。