强制装配正在加载? [英] Force assembly Loading?

查看:80
本文介绍了强制装配正在加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近制作了一个使用WebClient类从Web获取html响应的应用程序.有很多涉及获取html代码的调用,我已经注意到,第一次调用该应用程序需要等待大量时间才能将程序集加载到内存中(我想).

所以我做了以下...

I recently made an app that uses the WebClient class to get html responces from web. There are numerous calls that involve fetching html code and i have noticed that the first time a call is made the app waits a significant amount of time for loading the assembly into memory (i guess).

So i made the following...

public frmMain()
{
    InitializeComponent();
    ShowInTaskbar = false;
    WindowState = FormWindowState.Minimized;
    Visible = false;
    Thread startThread = new Thread(new ThreadStart(speedup));
    startThread.Start();
}

void speedup()
{
    try
    {
        WebClient wc = new WebClient();
        String Body = wc.DownloadString("http://www.google.com");
    }
    catch { }
    Invoke(new ThreadStart(showform));
}

void showform()
{
    Visible = true;
    WindowState = FormWindowState.Normal;
    ShowInTaskbar = true;
}



目前,这似乎可以正常工作,但是我不确定在应用程序运行时是否有间隔一定时间需要同一件事的可能性(我的意思是,如果有一段时间不使用该程序集,是否有可能将其卸载) )或通常是否有更好的方法.

就卸载"而言,我之所以这样说是因为我已经注意到,在应用程序保持不活动状态几分钟后,它又花费了足够长的时间来执行以下命令:
System.Net.WebClient.DownloadString(Url);
我做了一些测试,并且确定:
System.Net.WebClient wc =新的System.Net.WebClient();
不会在任何时候减慢我的应用程序的速度,但这是
DownloadString会这样做,并且第一次调用它需要5到10秒,在随后的调用中甚至是html代码更改的动态站点大约需要100毫秒(因此,我不能认为这是由某些缓存引起的.).



At the moment this seems to work fine but i am not sure if there is any chance that the same thing is needed in some intervals during application runtime ( i mean if there is any chance for the assembly to be unloaded if not used for some time) or generally if there is any better way of doing this.

As far as the "unloading" goes i am saying this because i have noticed that after the application stays inactive for a couple of minutes it again takes long enough process the command:
System.Net.WebClient.DownloadString(Url);
I did some testing and i am certain that the:
System.Net.WebClient wc = new System.Net.WebClient();
does not slow down my application at any point but it is the
DownloadString that does, and it takes 5 to 10 seconds for it the first time called and about 100 ms in subsequent calls even to dynamic sites that the html code changes ( therefore i cannot assume this is caused by some caching ).

推荐答案

将WebClient作为静态变量存储在您的类中.在静态构造函数中,创建WebClient的新实例,并将其分配给该静态变量.这应该足以加载程序集,并且将在第一次使用您的类时完成.我认为,当您持有该类的实例时,程序集将不会被卸载.

Store WebClient as a static variable on your class. In the static constructor, create a new instance of the WebClient and assign it to that static variable. That should be sufficient to load the assembly, and it will be done when your class is used for the first time. I would think that, while you are holding onto an instance of the class, the assembly would not be unloaded.

如果不卸载包含该程序集的所有应用程序域,则无法卸载该程序集.即使程序集超出范围,实际的程序集文件也将保持加载状态,直到卸载包含该程序集的所有应用程序域为止.

There is no way to unload an individual assembly without unloading all of the application domains that contain it. Even if the assembly goes out of scope, the actual assembly file will remain loaded until all application domains that contain it are unloaded.



这似乎意味着程序集一旦被您的应用程序加载就不会被卸载.



That seems to imply that the assembly will not be unloaded once it is loaded by your application.


如果您要调用ASP.NET网站,我知道有时网站本身会变成无效,新的页面请求将导致重新启动.这就是为什么有时页面在您访问的ASP.NET网站上加载缓慢的原因.诀窍是从网站上下载以使其处于活动状态,并定期从同一网站上下载以使其保持活动状态.

另外,对于使用ASP.NET以外的其他技术构建的网站,可能必须保持网站的活动状态,但是我确信ASP.NET网站存在此问题.
If you are making calls to an ASP.NET site, I know that sometimes the website itself becomes inactive and a new page request will cause to to startup again. This is why sometimes pages are slow to load on ASP.NET websites you visit. The trick would be to download from the website make it active and to download from that same website periodically to keep it active.

Also, this keeping websites alive may be necessary for websites that are built on some technology other than ASP.NET, but I am sure this problem exists with ASP.NET websites.


这篇关于强制装配正在加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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