如何在Dot Net Compact Framework中预加载类库?

发布于 2024-09-10 06:35:20 字数 687 浏览 2 评论 0原文

假设我加载 Form1,然后它显示了让我访问 Form 2,3 和 4 的按钮。

现在,当我单击按钮加载 Form2 时,我可以看到加载一些类库(. dll),这使得 UI 看起来没有响应。

在加载类库之前我看不到表单,这使得 Form1 在加载库之前仍然显示。但在那之后,速度相当快。

那么我如何预加载类库,也许是在应用程序启动时?

我尝试将 using 语句放入 Form1,紧凑的框架非常好,以至于在 Form2 实际需要它之前它不会加载。我说的是 2 秒的延迟,但看起来仍然很糟糕。

有办法克服这个问题吗?我不能强制CF加载我的dll文件吗?它正在加载System.dll、System.Windows.Forms.dll等?

更新:

我可以使用加载我自己的类库 时我仍然无法加载以下文件

Assembly.LoadFrom 但在 Form1 加载Microsoft.WindowsMo​​bile.PocketOutlook.dll
Microsoft.WindowsCE.Forms.dll
Microsoft.WindowsMo​​bile.Utilities.dll
System.Xml.dll

这可能吗? :)

Lets say i load Form1 and then it shows buttons which let me access Form 2,3 and 4.

Now, when i click the button to load Form2, I can see that it takes a bit of time to load a few class libraries (.dll) which makes the UI look unresponsive.

I don't see the form until the class libraries are loaded which makes the Form1 to still be shown until the libraries are loaded. But after that, it is pretty fast.

So how do i pre-load the class libraries, perhaps when the application is started?

I tried putting the using statements to the Form1 and the compact framework is so good that it wont load until it is actually required by Form2. I am talking about 2 seconds of delay but still looks bad.

Is there anyway to overcome this problem? Cant i force the CF to load my dll's file it is loading System.dll, System.Windows.Forms.dll etc?

UPDATE:

I could load my own class libraries using
Assembly.LoadFrom but I am still unable to load the following files when Form1 loads

Microsoft.WindowsMobile.PocketOutlook.dll
Microsoft.WindowsCE.Forms.dll
Microsoft.WindowsMobile.Utilities.dll
System.Xml.dll

Is it even possible? :)

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

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

发布评论

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

评论(1

腻橙味 2024-09-17 06:35:20

它不是关于“加载类库”,而是关于需要 JITted 的代码(运行时从 IL 编译为机器代码)。第一次显示表单时,运行时必须 JIT 所有代码来创建类实例。它还必须运行构造函数等。随后,它可能只显示表单,即使它必须加载新实例,JITted 代码也可能已经被缓存并准备好运行,这意味着不需要编译。

为了获得最佳的“可见”性能,您希望将用户单击按钮时所需的 JIT 量保持在最低限度。如果您的表单需要某些服务或对象,请将它们预加载到后台线程中,以便它们在表单需要时准备就绪。

如果不详细了解您正在加载的内容、加载时间以及表单实际需要显示的内容,就很难为您提供更详细的指导。

编辑

它可能看起来像这样:

class MyForm
{
    static void Main()
    {
        new Thread(delegate
            {
                AppInitialize();
            })
            {
                IsBackground = true
            }
            .Start();

        Application.Run(new MyForm());
    }

    static void AppInitialize()
    {
       // load app-wide resources, services, etc
    }

    public MyForm()
    {
        InitializeComponent();

        ThreadPool.QueueUserWorkItem(
            delegate
            {
                InitializeServices();
            });
    }

    void InitializeServices()
    {
        // load up stuff the Form will need after loading/first rendering
    }
}

同样,由于我不知道您在做什么,因此您的应用程序可能需要有所不同。我还使用 IoC 容器框架,因此我的代码看起来有很大不同。不过,基本上我使用相同的概念。

It's not about "loading a class library" it's about what code needs to be JITted (runtime compiled from IL to machine code). THe first time you show your form, the runtime has to JIT all of the code to create your class instances. It also has to run the constructor, etc. Subsequent times, it may only be showing the form, and even if it has to load a new instance, the JITted code may already be cached and ready to run, meaning no compile is necessary.

To get the best "visible" performance, you want to keep the amount of JITting necessary when the user click a button to a minimum. If your Form is going to need some services or objects, pre-load them in a background thread so they are ready when the Form needs them.

Without knowing a bit more about exactly what you're loading, when you're loading it, and what your Form's actually need to display, it's difficult to give you more detailed direction.

Edit

It might look something like this:

class MyForm
{
    static void Main()
    {
        new Thread(delegate
            {
                AppInitialize();
            })
            {
                IsBackground = true
            }
            .Start();

        Application.Run(new MyForm());
    }

    static void AppInitialize()
    {
       // load app-wide resources, services, etc
    }

    public MyForm()
    {
        InitializeComponent();

        ThreadPool.QueueUserWorkItem(
            delegate
            {
                InitializeServices();
            });
    }

    void InitializeServices()
    {
        // load up stuff the Form will need after loading/first rendering
    }
}

Again, since I have no idea what you're doing, it may need to be different for your application. I also use an IoC container framework, so my code looks a lot different. Fundamentally I use the same concepts, though.

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