TaskFactory.StartNew in Page Load

233 Views Asked by At

In my web application, I have a page which is just for displaying purpose. After verifying the data, users can download the reports, which is taking around 2 - 3 minutes of time. I need to reduce this time taken. So, I thought of starting the report generation once the page is called, to share the time, without affecting the Page Load. I tried executing the report generation method simultaneously by using Task.Factory.StartNew() in my Page Load. The thing is that, I am able to execute the method as I expected. But, it shows the page, only when report generation is completed, even though page load event is over. Is it possible to force the page to be shown, once the page load is completed, and continue the report generation process in behind? Here is my code,

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        // page load condition checks

        Task.Factory.StartNew(() => GenerateReports(), CancellationToken.None, 
             TaskCreationOptions.None, TaskScheduler.Default);
        //
    }
}
1

There are 1 best solutions below

3
On

Does this give u same results?

Task GenReport = Task.Run (() =>
{
    GenerateReports();
});

Edit: Try putting it in a new method and calling the method from pageload

Edit2: As Aron suggested dont use task run. I was just trying to help with what I knew in c# not asp. If I think further about it.. it can probably overload your server if client hit refresh too many times, that would create multiple tasks...