I am trying to cancel current request made by user. Web Forms Web App have export to excel feature. If it takes long time to generate excel report user can cancel the request.
Once the cancel button is clicked the server should stop processing to save precious bandwidth.
I have found two solutions as listed below.
First Solution:
Prevent further code from executing by jumping straight to the Application_EndRequest
event.
protected void btnCancel_Click(object sender, EventArgs e)
{
if (Response.IsClientConnected)
{
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
Second Solution: (Using Task Parallel Library - TPL)
CancellationTokenSource tokenSource = new CancellationTokenSource();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnExportExcel_Click(object sender, EventArgs e)
{
CancellationToken cToken = tokenSource.Token;
Task.Factory.StartNew(() =>
{
// do some heavy work here
GenerateReport(sender, cToken);
if (cToken.IsCancellationRequested)
{
// another thread decided to cancel
return;
}
}, cToken);
// to register a delegate for a callback when a
// cancellation request is made
cToken.Register(() => cancelNotification());
}
private void GenerateReport(object sender, CancellationToken ct)
{
var students = _context.Students.ToList();
var courses = _context.Courses.ToList();
var teachers = _context.Teachers.ToList();
// Todo: Use above data to Generate Excel Report
// Just to Simulate Export to excel
Thread.Sleep(7000);
}
protected void btnCancel_Click(object sender, EventArgs e)
{
tokenSource.Cancel();
}
private static void cancelNotification()
{
// Why never called ?
}
In second solution I have used Task Parallel Library (TPL) and registered a callback method on cancellation using CancellationToken
. I have few questions:
But the callback method is never called when I click cancel button, may I know why ?
Using
Entity framework
can I cancel all 3.ToList()
statements before it hits the database to fetch the data.Also may I know is it best option to go with
TPL
?
Any help would be appreciated.