Click here to Skip to main content
15,941,444 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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...
C#
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;
}


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 ).
Posted
Updated 23-Feb-10 22:37pm
v2

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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900