Asynchronous Method Calls in ASP.NET/VB.NET

Published 12.12.2005 by ~mattg

The VB.NET app that I “inherited” as part of a consulting job I’m doing isn’t without it’s quirks. To remedy some of the problems we were having, we quoted and performed a refactoring of the photo gallery sections of the application. Prior to the refactoring, all of the images were stored in the DB and used a custom image control that was able to display the images given a byte array (I’m assuming by writing the bytes to a temporary image file). This made trying to implement things like Javascript slideshows impossible without letting javascript access the DB, which I was vehemently against.

So now, the images are saved to disk, and the application dynamically “caches’ different size images (thumbs, viewable images, slideshow slides) as needed. This works for most of the viewable and thumbnail images, since only about 6 images (at most) must be created at one time.

For the slideshow, however, I need the URL to all of the cached images to create the javascript. I had a function that cached the photo and returned a url (which came from a pretty simply file-naming scheme). This was causing a problem because large albums (many images) would take a while to create the actual files, when all I really needed was the filename synchronously. I needed some way to create the slide images in the background.

In comes Asynchronous calls. Since I use a very simple naming scheme, I wrote a function to return the name of the file without actually checking to make sure it was there. That way, I could create the slideshow script without caching any files. So I cache the first three slideshow images synchronously (to make sure they are there) and then make an asynchronous call to a function that loads the rest of the slideshow images. Meanwhile, the page is loaded and the response time is constant.

I used the VB.NET version of the BackgroundWorker class for .NET 1.1, which was implemented by Mark Jackson. You can get the source code, demo code, and explanation at The Code Project.

Filed under News

Comments (0)

Comments RSS - Trackback - Write Comment

No comments yet

Write Comment