I have a asp.net 2.0 web app which calls a long running stored proc (about 3 minutes). The sp actually performs many tasks on the backend.
My page uses threading and ajax (update panel) and a timer control to dispaly a progess bar to the user. This all works great unless there is an error in the sp which just freezes the screen.
I have tried useing a SafeThread class which I found on codeProject which wraps the thread process and creates an event which can be handled in the case of an exception.
In the event handler I just want to redirect the user to an error page and display an error. When testing I can break in the event handler. Calling either Server.Transfer or Response.Redirect however, has no effect at all.
I am not sure why this happenning. I will post my code below. Any ideas or alternative suggestions appreciated.
protected void btnSave_OnClick(object sender, EventArgs e) { DAC dac = new DAC(); Session['RerunStatus'] = 0; SafeThread thrd = new SafeThread(new ParameterizedThreadStart(dac.RerunTest)); thrd.IsBackground = true; //tell the SafeThread to report //ThreadAbort exceptions thrd.ShouldReportThreadAbort = true; //attach a ThreadException handler for //reporting SafeThread exceptions thrd.ThreadException += new ThreadThrewExceptionHandler(thrd_ThreadException); thrd.Start(ddlRundate.SelectedItem.Text); Session['RerunThread'] = thrd; btnSave.Enabled = false; Timer1.Enabled = true; } void thrd_ThreadException(SafeThread thrd, Exception ex) { //thrd.Abort(); Timer1.Enabled = false; //Response.Redirect('ErrorPage.aspx'); Server.Transfer('ErrorPage.aspx?ErrorMsg=' + ex.Message); //thrd.Abort(); }
You can only use Server.Transfer()/Response.Redirect() when handling a request – that is not the case when your thrd_ThreadException() get’s called.
Try to set a flag in your thrd_ThreadException and do the redirect in the Timer1 Tick event.