Communication With a Background Thread in ASP.NET

Over the past couple of years I have written and presented on custom HttpModules. One of my examples is how to spin up a background thread to perform work not related to processing a web request. I thought I would extend the usefulness of this technique by showing how to communicate back and forth with a potential the user.

This example has a lot of moving parts. The first part is a custom HttpModule that launches a process in a background thread:

public

void

Init(HttpApplication context){ Thread t =

new

Thread(BkgProcess.DoWork); t.Start(context.Application);}

And of course a DoWork method that does something. In this case I am generating a random number between 1000 and 10000. This number is used as a value to put the thread to sleep or a random delay. It is also being used to set a value that is stored in the site's application context.

public

static

void

DoWork(

object

_app){ HttpApplicationState app = (HttpApplicationState)_app;

if

(

null

!= app) {

int

curVal = 1000;

if

(

null

== app[

"curVal"

]) { app[

"curVal"

] = curVal; }

int

i = 0;

while

(

true

) { Random rnd1 =

new

Random(curVal); curVal = rnd1.Next(1000, 10000);

lock

(app[

"curVal"

]) { app[

"curVal"

] = curVal; } Thread.Sleep(curVal); i++;

if

(i > 100) {

break

; } } }}

There are a couple of things that need to be explained about this method. First it accepts an object _app. It is cast to an HttpApplicationState object, which is accessed in the module's Init method from the current HttpContext.

Next when the random value is stored in the application context it is locked, which prevents a potential overwrite by another thread. In this demo it is not a real concern, but in the real world it will be in most cases.

Another thing I did was add a counter to keep this loop from running forever. I did this for demo purposes only.

I am into jQuery and AJAX lately, so that is what we will do for this demo. All the HTML for the page is a simple H1 title and an empty paragraph. This paragraph will be used to hold the randomly generated value from the background thread:

<

h1

>

Background Value

</

h1

>

<

p

id

="curVal"

></

p

>

The AJAX for the demon uses the .ajax method of the jQuery framework to call a generic HttpHandler, with a success option that sets the content of the paragraph above. I also added a little pulsing feature (fadeIn and fadeOut).

$(document).ready(

function

() { GetValue();});

function

GetValue(){ $(

'#curVal'

).fadeOut(750); $.ajax({ type:

"GET"

, url:

"GetCurrentValue.ashx"

, dataType:

"text"

, success:

function

(curVal) { $(

'#curVal'

).html(

'The current value is: '

+ curVal); $(

'#curVal'

).fadeIn(750);

var

retryInt = setTimeout(

"GetValue()"

, 2500); }, error:

function

(curVal) { $(

'DIV'

).innerHTML =

"error "

+ curVal; } });}

The Generic HttpHandler accesses the random value from the application context and writes it to the Request for the ajax call to consume.

public

void

ProcessRequest (HttpContext context) { context.Response.ContentType =

"text/plain"

;

if

(

null

!= context.Application[

"curVal"

]) { context.Response.Write(context.Application[

"curVal"

]); }

else

{ context.Response.Write(

"1"

); } context.Response.Flush(); context.Response.End(); }

Once you put all this together you get a randomly generated number on the page that will update as soon as the value changes in the background thread.

You can download the source for this demo here.

Share This Article With Your Friends!