为什么当我使用 Process.Start() 时我的应用程序启动时间变慢?

发布于 2024-09-16 01:31:54 字数 221 浏览 4 评论 0原文

我正在尝试分析应用程序的启动时间,因此我编写了一个小型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦明 2024-09-23 01:31:54

感谢您的帮助。我有答案,它与 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.

万水千山粽是情ミ 2024-09-23 01:31:54

您的线索应该是 Process.Start() 位于 System.Diagnostics 命名空间中。当您以这种方式启动一个流程时,您将向其附加一堆监视器/检查器。这肯定会增加开销。

您可能想在启动 Process 对象后尝试立即调用 Dispose() (以避免不必要地延长进程监控),但您将无法完全避免相关的管理费用。

Your clue should be that Process.Start() is in the System.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 the Process 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.

飘逸的'云 2024-09-23 01:31:54

简而言之,您实际上正在启动两个进程,而不仅仅是一个进程。这就是为什么需要更长的时间。

当您双击您的应用程序时,您只会加载一个应用程序及其所有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文