Slow first Time in ASP.NET applications

19. March 2013 12:19 by Mrojas in   //  Tags: , , , , , ,   //   Comments (0)

Well, this is a recurrent topic with ASP.NET:Why is the first page load always so slowjQuery152032498610322363675_1363786231988?

I don't think there is a definite answer but I can think on some explanations:

  • ASP.NET applications by nature exhibit some degree of delay upon initial access to the site. This can be due to JIT compilation, caching, etc. 
  • Note that it is also a common effect on some sharepoint sites. 
  • Some people on StackExchange attribute this slowness to: " The IIS application pool is shut down after 30 minutes of inactivity. After that, when you make a request IIS basically has to start the website up again, which leads to the behavior you are describing. You can change the idle time of your website in iis though to avoid it." 
Workarounds
 
 
There are some workarounds for this situation:
 
For some years there has been a warmup script that you can use on pre-vs2010 apps:
 

ASP.NET Site Warm up on GitHub

 
And there is  even an IIS addin for that, the following blog provides some references about this addin:
 

ASP .NET Pre heating and the App Warmup Addin for IIS

 
These problem is so common that now in VS 2010 there is even an auto-start feature that you can use:

Auto Start Feature in ASP.NET 4

 
 I hope this links help and I'll add more explanations as I find them. Please feel free to comment.

Silverlight ObservableCollection with AddRange

7. October 2011 07:00 by Mrojas in   //  Tags: , , , , , , , , , , ,   //   Comments (0)

Silverlight is when you have to add a lot of items.
I know, I know maybe you should choose another way to show that data,
but leaving philosophical-ui design discussions, the real problem is that
usually those components are bind  to ObservableCollections.

ObservableCollections are a bit of an exhibitionist.
Each time you add an item it will yell

Hey!! Yoo-hoo! HEY!!! YOU!!
I'm HEREEEEEEEEEEEEEEEEEE!!!!
Look at me!! Look at Me!!! Look Mom No Hands!!! Look Dad no Feet!!! HEY!!!!!!!!

So if you have some code like:

for(int i=0;i<10000;i++)
{
    comboItems.Add("item" + i);
}


A nice thing will be to be able to do something like:

var items = new String[10000] 
for(int i=0;i<10000;i++) 
{ 
    items[i]="item" + i; 
} 
comboItems.AddRange(items); 


And then provide just ONE notification of Collection Changed instead of a lot of
little cries for attention.

Well that is the reason for this new version of ObservableCollection that I call
RangeObservableCollection:

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Utils
{
    public class RangeObservableCollection<T> : ObservableCollection<T>
    {
        private bool _suppressNotification = false;

        public RangeObservableCollection() : base() { }

        public RangeObservableCollection(IEnumerable<T> collection) : base(collection) { }

        protected override void OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (!_suppressNotification) base.OnPropertyChanged(e);
        }

        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (!_suppressNotification)
                base.OnCollectionChanged(e);
        }

        /// <summary>
        /// Adds a collection suppressing notification per item and just raising a notification
        /// for the whole collection
        /// </summary>
        /// <param name="list"></param>
        public void AddRange(IEnumerable<T> list)
        {
            if (list == null) throw new ArgumentNullException("list");
            _suppressNotification = true;
            foreach (T item in list)
            {
                Add(item);
            }
            _suppressNotification = false;
            OnCollectionChanged(new System.Collections.Specialized.NotifyCollectionChangedEventArgs(System.Collections.Specialized.NotifyCollectionChangedAction.Reset));
        }

    }
    
}

Categories