First, some background thoughts:
1) Since it's a long running job, you may want a cache of previously executed jobs so that you aren't duplicating work and overburdening your server.
2) You shouldn't have to start a separate background thread, since each ASP.NET request is going to be executing in it's own thread anyway. However, if you find you are starving your ASP.NET worker process, you can make your web methods asynchronous on the server side - see: this article.
The implementation requires three components - a cache, a webmethod to run the job, and a web method to check job status. You will also need to be able to generate a key that allows the system to track requests. You can generate this server side (when you generate the page), or client side (if you have a clever algorithm that can generate unique ids).
The cache can be application scoped if you want users to be able to share running jobs, or session scoped if you don't. The cache is simply a dictionary that holds references to your running job instances.
A simple example (this won't compile, probably contains syntax errors, and is intended mostly to get the point across).
Server pseudo code:
//In application Session_Start,
Session["ReportCache"] = new Dictionary<string, ReportStatus>();
public class ReportResults
{
}
public class ReportStatus
{
public int PercentComplete = 0;
}
[WebMethod(EnableSessions = true)]
ReportResults RunReport(string uniqueid)
{
ReportStatus status = new ReportStatus();
Session["ReportStatus"].Add(uniqueid, status);
//Start report
for(int i = 0; i < 100000; ++i)
{
//update the report status
status.PercentComplete = 100 * 100000 * i / 100000;
}
Session["ReportStatus"].Remove(uniqueid);
}
[WebMethod(EnableSessions=true)]
public ReportStatus GetStatus(uniqueid)
{
try
{
return Session["ReportStatus"][uniqueid];
}
catch(Exception)
{
return null;
}
}
On the client, use ajax to call the first web method. It won't call the onSuccess callback until the report is finished. Use setTimeout to periodically poll the second web method to get status information and update the client. When the first web method completes, cancel your timer.
Your biggest problem is going to be the fact that your server is going to be running lots of long running jobs, which can degrade overall performance and responsiveness. You might want to take a different approach altogether. For instance, rather than running the reports on demand per user, you can queue up the list of people who want to receive the report - then, once an hour/day/week (whatever) you can have a separate process run the report once and send it out.