如何在初始屏幕上报告将程序集加载到.Net中的当前AppDomain中的进度? [英] How do I report progress on loading assemblies into the Current AppDomain in .Net on a splash screen?

查看:58
本文介绍了如何在初始屏幕上报告将程序集加载到.Net中的当前AppDomain中的进度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在显示将程序集加载到AppDomain.CurrentDomain中的进度时遇到了一些困难.

I'm having a little difficulty in displaying the progress of assemblies being loaded into the AppDomain.CurrentDomain.

我想要做的是显示一个带有进度条的初始屏幕,我希望能够在每个程序集都加载到AppDomain的内存中时更新此进度条.这样,用户将可以直观地看到应用程序启动的进度.

What I want to do is display a splash screen that has a progress bar, I want to be able to update this Progress bar as each assembly is loaded into memory in the AppDomain. This way users will have a visual indicator as to the progress of the application launch.

我不在乎是否在呈现初始屏幕之前加载了程序集,该屏幕实际上只是一个带有进度条的标准WPF窗口.加载启动画面后,我想报告装配件.我不是手动加载这些程序集.

I don't care that assemblies are loaded before the Splash Screen is rendered which is actually just a standard WPF Window with a progress bar. Once a Splash Screen is loaded I would like to report the assemblies as they are loaded. I am not manually loading these Assemblies.

到目前为止,我已经使用以下标记处理了App.xaml中的Startup事件:

So far I have handled the Startup event in App.xaml with the following markup:

Startup="Application_Startup"

启动后对应的代码如下:

The corresponding code behind for startup is as follows:

private void Application_Startup(object sender, StartupEventArgs e)
{
SplashScreen.Show<SplashScreen>();
AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad;
}

void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
{
    SplashScreen.CallSplashScreenMethod<SplashScreen>(x => x.Text(args.LoadedAssembly.GetName().Name));
}

这按我预期的方式工作,将显示一个初始屏幕,并且在显示初始屏幕的那一刻正在加载的任何程序集都会毫无问题地显示在屏幕上.

This works as I expect it, a splash screen is displayed and whatever assemblies are being loaded at the moment the splash screen is displayed are showed on screen without a problem.

这里的问题是我看不到报告正在加载的程序集进度的方法.

The problem here is that I can't see a way of reporting progress of assemblies being loaded.

程序集似乎没有以任何特定的顺序线性加载,因此,我不确定如何对要加载的程序集的剩余数量进行计数.我没有一种明显的方法获取当前正在加载的程序集.

Assemblies don't seem to be loaded linearly in any particular order, due to this I am not sure how to go about getting a count of the remaining number of assemblies to be loaded. I can't see an obvious way to get what assemblies are currently being loaded.

如果我可以获得尚未由CLR加载的程序集列表,则可以通过使用标准计数或沿行实现进度条

If I could get a list of assemblies yet to be loaded by the CLR I could implement a progress bar by using either a standard count, along the lines

progress = numberOfLoadedAssemblies / totalNumberOfAssemblies * 100;

或者通过实现 Chris Marisic关于使用加权平均值的出色答案

我无法执行琐碎的" Directory.GetFiles().Length "以获取要加载的程序集总数.原因是因为CLR并未按任何特定顺序加载程序集.似乎是从应用程序根目录加载一些程序集,然后从GAC加载某些程序集,然后从应用程序根目录加载更多程序集.

I can't do a trivial "Directory.GetFiles().Length" To get the total number of assemblies to be loaded. The reason why is because the CLR isn't loading assemblies in any particular order. It seems to load some assemblies from the application root, then some from the GAC, then more from the application root.

我想知道的是,是否有办法获取要加载的其余程序集的列表.

What I'd like to know is if there is a way to get a list of remaining assemblies to be loaded.

推荐答案

正如其他人所述,您不能仅假设10个加载的组件中有9个表示90%.但是,您可以使这些假设更加现实.

As others have stated you can't just assume 9 assemblies out of 10 loaded means 90%. However you can make these assumptions alot more realistic.

扫描将要加载的所有程序集的文件信息,以便获得每个程序集的文件大小.现在为平均"装配创建一个加权值.

Scan the file info of all of the assemblies that will be loaded so you have the filesize of each assembly. Now create a weighted value for the "average" assembly.

var totalSize =  assemblies.Select(x=> x.FileSize).Sum();
var averageSize = totalSize / assemblies.Count();

var averageChange = averageSize / totalSize;

假设进度条的比例为0到1,则每次加载另一个程序集时都可以添加averageChange.

Assuming your progress bar is a scale of 0 to 1, you could just add averageChange each time another assembly is loaded.

或者您实际上可以计算加载的字节数与进度条的总大小.我个人将使用平均方法来防止进度条从单个程序集从[-]到[-------------------].用户不习惯非线性变化,也不倾向于信任非线性变化.给他们一个线性增加的进度条,该进度条几乎完全在加载完成时结束,从而使人们对事物的工作有适当的感觉,并且很快就会完成.进度条中不稳定的跳转将使用户感觉 系统不稳定且不可信.

Or you could actually count the bytes loaded vs the total size for the progress bar. I personally would go with my averaged method to prevent the progress bar going from [--] to [-------------------] from a single assembly. Users are not accustomed to non-linear changes and are not prone to trust non-linear changes. Giving them a linearly increasing progress bar that ends almost exactly when it's done loading gives people the proper feelings of things are working and will be done soon. Erratic jumps in progress bars will make the users feel the system is erratic and untrustworthy.

我不能低估的一件事是,用进度条激怒用户是多么容易.没有什么比进度栏快速填充命中率和停止率更能激怒用户了.这使他们感到程序冻结.他们还需要视觉指示,在任何仪表之间,事物还没有冻结.他们也不想长时间坐着(请稍等片刻),看到进度条已满,然后又变回零.

One thing I cannot understate is how easy it is to anger your users with a progress bar. Nothing angers users more than a progress bar that rapidly fills hits 99% and halts. It makes them feel like the program froze. They also need visual indication that between any meter that things have not frozen. They also don't want to sit around for a long time (this is seconds mind you) see a progress bar fill and then flip back to zero.

这篇关于如何在初始屏幕上报告将程序集加载到.Net中的当前AppDomain中的进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆