1

I am using c# method Image.GetThumbnail() to generate thumbnail of an Image. I have to generate this thumbnail dynamically. I have to generate 100 thumbnails for a single galleryId. So I added an HttpHandler to generate the thumbnail dynamically. The problem is when I click a gallery Id There is 100 request goes to my Http handler. So the thumbnails loads very slowly. I have some questios

  1. Can I get the performance improvement with the implementation of Asynchronous Http Handler? I am not familiar with the asynchronous programming in c#. How Can I generate thumbnail using Http Asynchronous Handler?
  2. Is there any alternative way to get better performance than asynchronous programming model? I mean add multiple handlers for serving the request like

Can anyone please help me.

KiranPalode
  • 498
  • 3
  • 8
  • 23

2 Answers2

2

Another way to solve this problem is to avoid it in the first place.

Generate the thumbnail when the image is uploaded and then just serve the ready thumbnail with content expiry set appropriately.

You will save quite a lot of processing and, what is more important, shift it in time, so when users are viewing the gallery you can serve the thumbnail as quickly as possible.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • Sorry. I have no option to upload the files. I have a directory which is shared in the server from where I take the images. Second one is I should create the dynamic thumbnails only. Could you Please share your ideas if you have a better solution to resolve the problem. – KiranPalode May 21 '12 at 07:53
1

Here you need to define the real issue of the delay. Is because you call it 100 times at the same moment, or is because your handler is blocked by session lock ?

So first think is to remove the session from your handler - if you use it.

Second, if your problem is because you call it many times together you can limit this by using mutex and a simple trick. You can lock the handler to simulate only create let say 6 thumbnails simultaneously using mutex.

Here is a simple code that use mutex and can left at the same time n threads to run

static var random = new Random(DateTime.Now.Ticks);

public void ProcessRequest_NoCatch (HttpContext context) 
{
    // here we made names like ThubNum_0, ThubNum_1, ThubNum_2 ... ThubNum_4
    //  with 4 you have average 4 simulated thubs
    string sMyMutexName = string.Format("ThubNum_{0}", random.Next(0, 4))

    var mut = new Mutex(true, sMyMutexName);

    try
    {   
        // Wait until it is safe to enter.
        mut.WaitOne();

        // here you create your thubs
    }
    finally
    {
        // Release the Mutex.
        mut.ReleaseMutex();
    }   
}

See how session block each other pages: Web app blocked while processing another web app on sharing same session

Replacing ASP.Net's session entirely

cache

Of cource you need to cache your thumbnail's to the disk, and set also cache for the images for the browser. There is no reason to create them again and again.

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • No I am not implementing the IRequiresSessionState. Then Do we need to remove the session? How can I remove session from my Http Handler? When I click the Gallery ID I gets a list of 100 image url via ajax. Then I set my 100 img attribute's src with this urls like via javascript. image.ashx is my handler which generates thumbnail. Then 100 requests are gone to take the thumbnails. This is my scenario. How can I implement the mutex method in my handler. Please help me. – KiranPalode May 20 '12 at 18:46
  • @KiranPalode Ok, the next think is to add mutex. Start by adding one mutex that left only one thumbnail to made and see if this improve it. Gen this example http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx if you do not understand it tell me to write the code – Aristos May 20 '12 at 18:58
  • It takes more time..It doesn't shows the thumbnails after a long time. I think the implementation became a long running task. Could you please share the exact idea what you mean. – KiranPalode May 21 '12 at 07:50
  • @KiranPalode Then your problem is that you do not cache the thumbnails. You can not do anything else but cache them. – Aristos May 21 '12 at 08:23
  • OK. I can add output caching mechanism here.But the problem is Initial Request.From second click onwards we can get the benefit of caching. But, When I click the first time I have to wait little time. I have to get a better performance here ie. First time load. – KiranPalode May 21 '12 at 09:54
  • @KiranPalode cache cache cache, on disk... I create more than 100 thub in a page, but with cache. The only delay was on the first days that the thumbnails where created, then the new image are not add so much overhead... apparently you do not cache / save the thumbnails to the disk... save them, make them cache and your issue will be solve. – Aristos May 21 '12 at 10:19