0
我正在开发一个Windows窗体启动器/更新/启动画面应用程序,用于C#中的Windows 7松下Toughbook(平板电脑)。发射器在我的桌面和我的Toughbooks之一上工作正常......但是,在“真实环境”设备上进行测试需要20多秒才能在首次启动后显示该表单。 (一旦显示表格,它就会很快,应用程序假设可以工作。)安装后首次在设备上启动很快,但首次启动后需要很长时间。C#Windows窗体永远加载
我不确定如何去测试,因为这是一个无法通过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();
}
“悬挂”20秒感觉很像某种超时。你提到设备不在网络上。是否有一些代码(可能从开发中遗留下来?)试图打开网络资源? updateApplication()是否通过网络检查? – 2013-04-30 15:49:11
它通过无线网卡连接到不在我们网络上的服务器。有趣的是,表格在20秒内完全不显示。一旦表格显示出来,它就可以做到这一切。 – teynon 2013-04-30 15:50:28
如果某些内容(如网络调用...)阻塞了消息队列,则表单不会显示20秒。如果你做了一些需要一段时间的事情,可以在一个单独的线程中完成(BackgroundWorker通常在WinForms上下文中运行良好)。 – 2013-04-30 15:52:00