如何减少 .net 应用程序的启动和运行时间?

发布于 2024-08-18 04:34:12 字数 215 浏览 5 评论 0原文

我使用 C# 和 .NET 框架编写 Windows 应用程序。如何减少这些应用程序的启动时间?我的应用程序在启动和初始化期间感觉非常慢,特别是在显示初始表单时。

我的应用程序使用 Access (MDB) 数据库来保存数据。在某些表单中,它会加载数据,但应用程序第一次显示任何给定表单时,需要很长时间才能显示。

我尝试过使用 NGen 来减少启动时间,但它并没有像预期的那样帮助我。

I use C# to write windows applications with the .NET framework. How can I decrease startup time for these applications? My applications feel very slow during startup and initialization, particularly when showing the initial form.

My application works with an Access (MDB) database to save data. In some forms it loads data, but the first time the application shows any given form, it takes a long time to display.

I've tried using NGen to decrease the startup time, but it did not help me as expected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

奢望 2024-08-25 04:34:12

确保您的应用程序在启动时执行尽可能少的工作。

您可以更改启动代码以将启动任务推迟到辅助线程。

我会给出更详细的答案,但你的问题太宽泛而且不够详细。

Make sure your application does as little work as possible on startup.

You can change your startup code to defer startup tasks to a secondary thread.

I'd give a more detailed answer, but your question is very broad and not detailed enough.

╰◇生如夏花灿烂 2024-08-25 04:34:12

首先,您需要诊断在此启动期间正在运行哪些功能,并分析每个功能消耗了多少启动延迟...

然后一旦您知道,是否其中一两个功能消耗了大部分时间,并且不涉及最初显示的 UI 屏幕,在单独的线程上执行这些功能。

First you need to diagnose what functions are running during this startup period, and analyze how much of the startup delay each of the functions is consuming...

Then once you know that, if one or two of them are consuming the bulk of the time, and do not involve the initially displayed UI screens, execute those functions on a separate thread.

新一帅帅 2024-08-25 04:34:12

研究一下延迟加载。这涉及在第一次请求数据时加载数据并填充类,而不是在实例化类时。

// member variable
        private DataSet _employeeDataSet = null;

    // property
    public DataSet EmployeeDataSet
    {
        get
        {
            if (_employeeDataSet == null)
                _employeeDataSet = GetEmployeeDataSet();

            return _employeeDataSet;
        }
    }

Look into lazy loading. This involve loading data and populating a class when the data is first requested, rather than when the class is instantiated.

// member variable
        private DataSet _employeeDataSet = null;

    // property
    public DataSet EmployeeDataSet
    {
        get
        {
            if (_employeeDataSet == null)
                _employeeDataSet = GetEmployeeDataSet();

            return _employeeDataSet;
        }
    }
孤者何惧 2024-08-25 04:34:12

您可能需要使用 EQATEC Profiler 来分析您的应用程序并查看哪些函数调用需要很长时间。它可以更轻松地可视化后台发生的情况以及可以进行改进的地方。

You might want to use EQATEC Profiler to profile your application and see which function calls take a long time. It makes it a bit easier to visualize what is happening in the background and where improvements could be made.

丢了幸福的猪 2024-08-25 04:34:12

我所在的团队正在用 C# 构建 .NET 应用程序,我们一直遇到这种情况。有一个分析器,但没有人愿意使用它,因为 1)它有点麻烦,2)输出令人困惑。我所做的就是在 IDE 下启动它,虽然它非常慢,但我只是暂停它并通过检查调用堆栈来询问它在做什么以及为什么。

由于它比预期要慢,这是因为它正在做一些将被删除或以不同方式完成的事情。这些事情通常包括请求诸如以下内容的函数调用:国际化不需要国际化的字符串、多次加载内容、初始化只是为了完整性而存在且稍后将被替换的数据结构、解压缩和压缩超过的内容必要的。所有这些都采取中间堆栈函数/方法调用的形式,当它们出现在多个堆栈样本上时,只是迫切需要关注。

尝试预见这些问题而不是把它们放进去是件好事,但你可以指望这些问题会悄悄出现,尽管你的意图是好的。 堆栈采样(stackshots)的方法可以有效地发现他们。

I'm on a team building a .NET application in C#, and we run into this all the time. There is a profiler, but nobody bothers to use it because 1) it's a bit of a bother, and 2) the output is confusing. What I do is start it under the IDE, and while it is being piggishly slow, I just pause it and ask it what it is doing and why, by examining the call stack.

Since it is being slower than it's going to be, it is because it is doing some things that will be removed or done differently. These things generally consist of function calls requesting things like: internationalizing strings that don't need to be internationalized, loading things multiple times, initializing data structures that are there just for completeness and are going to be replaced later, unzipping and zipping things more than necessary. All of these take the form of mid-stack function/method calls and when they show up on multiple stack samples are simply crying out to be attended to.

It is good to try to foresee these problems and not put them in, but you can count on such problems creeping in, despite best intentions. The method of stack sampling (stackshots) is effective at finding them.

夏九 2024-08-25 04:34:12

这篇 MSDN 文章 有很多有用的提示,特别是对于 .NET 2.0应用程序。

我学到的最有用的事情是,在父表单上调用 SuspendLayout() / ResumeLayout() 是不够的,您可能需要调用 BeginUpdate() / EndUpdate()< /code> 包含许多子对象的子控件(例如树,或者在我的例子中 Infragistics ToolbarManagers)。在我的 ToolbarManager 上使用 Begin / EndUpdate 缩短了 3 秒的启动时间!

This MSDN article has lots of useful tips, particularly for .NET 2.0 apps.

Most useful thing that I learned there was that it is not enough to call SuspendLayout() / ResumeLayout() on the parent Form, you may need to call BeginUpdate() / EndUpdate() on child controls which contain many child objects (eg trees, or in my case Infragistics ToolbarManagers). Using Begin / EndUpdate on my ToolbarManager cut 3 seconds off startup time!

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