C# WPF 应用程序启动很慢

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16294624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:42:23  来源:igfitidea点击:

C# WPF Very slow application launch

c#wpfperformanceoptimization

提问by Roman Nazarkin

I've wrote a simple .net WPFapplication(contains only 2 small windows), but its launch is too slow - about 10-20 seconds!

我写了一个简单的.net WPF应用程序(只包含 2 个小窗口),但它的启动速度太慢了 - 大约 10-20 秒!

Profiler says:

Profiler 说:

  • Main->RunInternal(56%)
  • Main->RunInternal->ctor->LoadBaml(32%)
  • Main->RunInternal(56%)
  • Main->RunInternal->ctor->LoadBaml(32%)

Biggest part of application load time - is body of Main->RunInternalfunction, this isn't my function and i don't know what they makes. Can their execution time somehow be optimized?

应用程序加载时间的最大部分 - 是Main->RunInternal函数体,这不是我的函数,我不知道它们是做什么的。他们的执行时间可以以某种方式优化吗?

Loading Baml markup it takes 32% of all time, but my program have only 3 XAML files and they are containing less than 100 lines of code. Why does this action take so long?

加载 Baml 标记需要 32% 的时间,但我的程序只有 3 个 XAML 文件,它们包含的代码不到 100 行。为什么这个动作需要这么长时间?

Before asking I have read and tried these tricks, but they didn't help me:

在问之前,我已经阅读并尝试了这些技巧,但它们对我没有帮助:

So, how can I speed up the start time of my application?

那么,如何加快应用程序的启动时间?

Thanks.

谢谢。

PS.I've tested this program on two similar computers and the result is a same.

附注。我已经在两台类似的计算机上测试了这个程序,结果是一样的。

回答by Boklucius

Look at the article mentioned in the comments above.

看看上面评论中提到的文章。

I would suppose it is 8, (Authenticode), it depends on what assemblies you are referencing

我想它是 8,(Authenticode),这取决于您引用的程序集

回答by user2127480

I have the same problem. Google with RunInternal gives me nothing.

我也有同样的问题。带有 RunInternal 的 Google 什么也没给我。

I then tried to backup my previous codes and delete assemblies and files until there was only a window left.

然后我尝试备份我以前的代码并删除程序集和文件,直到只剩下一个窗口。

然后它起作用了——启动时间从 10 秒缩短到 1 秒!

This time, the profiler showed only System.Windows.Application.Run()instead of RunInternal

这次,探查器仅显示System.Windows.Application.Run()而不是 RunInternal

It's very strange that for this computer, even after re-importing the bugged(slow) version, it worked correctly! whereas my laptop, which did not go through the fixing above still boots the program for 10+ sec.

很奇怪,对于这台电脑,即使重新导入了有问题的(慢)版本,它也能正常工作!而我的笔记本电脑,没有经过上面的修复,仍然可以启动程序 10 秒以上。

回答by Alexey Shevelyov

My problem was that the project itself was located on the network drive. So the app has to do a whole round trip to the destination drive located 500 miles away from my location and back, compile everything - so surely it was taking all the time. I looked at other issues but this was the one in my case.

我的问题是项目本身位于网络驱动器上。因此,该应用程序必须往返于距我所在位置 500 英里的目的地驱动器并返回,编译所有内容 - 所以它肯定花费了所有时间。我查看了其他问题,但这是我的案例。

Make sure you project is located locallybefore you try any other things.

在尝试任何其他事情之前,请确保您的项目位于本地