为什么当我使用 Process.Start() 时我的应用程序启动时间变慢?
我正在尝试分析应用程序的启动时间,因此我编写了一个小型 C# 程序,它将使用 Process.Start() 方法启动我的应用程序,并使用秒表计时。
当我尝试自己启动应用程序(只需单击它)时,可能需要 2-3 秒。当我尝试使用我的测试程序启动应用程序时,需要 8-10 秒。启动时间始终存在如此大的差异。
知道为什么使用 Process.Start 启动可执行文件会对启动时间产生如此大的影响吗?
I'm trying to profile the startup time of my application, so I wrote a small C# program that will start my application using the Process.Start() method, and time it using a stopwatch.
When I try to start the application myself (by just clicking on it), it probably takes 2-3 seconds. When I try to start the application using my test program, it takes 8-10 seconds. The startup time consistently differs in that magnitude.
Any idea why using Process.Start to start an executable would affect startup times so much?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您的帮助。我有答案,它与 Process.Start 无关。
启动该过程后,我正在等待特定的窗口句柄出现,以知道该应用程序实际出现。环太紧了。我在 while 循环中引入了 200 毫秒的睡眠,启动时间又正常了。
Thanks for all your help. I have the answer, and it's unrelated to Process.Start.
After I start the process, I was waiting for a specific window handle to appear to know that the app actually showed up. The loop was too tight. I introduced a 200 ms sleep in the while loop, and startup time was normal again.
您的线索应该是
Process.Start()
位于System.Diagnostics
命名空间中。当您以这种方式启动一个流程时,您将向其附加一堆监视器/检查器。这肯定会增加开销。您可能想在启动
Process
对象后尝试立即调用Dispose()
(以避免不必要地延长进程监控),但您将无法完全避免相关的管理费用。Your clue should be that
Process.Start()
is in theSystem.Diagnostics
namespace. When you start a process in this manner, you're attaching a bunch of monitors/inspectors to it. This definitely adds an overhead.You might want to try immediately calling
Dispose()
on theProcess
object after you start it (in order to avoid unnecessarily-prolonged process monitoring), but you will not be able to completely avoid the associated overheads.简而言之,您实际上正在启动两个进程,而不仅仅是一个进程。这就是为什么需要更长的时间。
当您双击您的应用程序时,您只会加载一个应用程序及其所有 DLL。
当您运行诊断应用程序时,您首先加载第一个应用程序及其必须进行 JIT 的 .NET 程序集(即时编译:这不是免费的)。只有全部完成后,其他应用程序才能启动。如果您的目标应用程序也是 .NET 应用程序,则整个循环会重复进行。
Simply put, you are starting actually two process's not just one. That's why it takes longer.
When you double click on your app, you are loading just one application and all it's DLL's.
When you run your diagnostic app, you first are loading the first application with it's .NET assemblies which have to be JIT'd (Just in time compilation: which is not free). Only then after that is all done, then the OTHER application gets to start. If your target app is also a .NET app, then the whole cycle repeats itself.