Quantcast
Channel: Can a BackgroundWorker call RunWorkerCompleted events from an ASP.NET Web application?
Viewing all articles
Browse latest Browse all 8

Can a BackgroundWorker call RunWorkerCompleted events from an ASP.NET Web application?

$
0
0

Hello,

My RunWorkerCompleted event handler is not called for a BackgroundWorker in an ASP.NET Web application.

The BackgroundWorker is created during an asynchronous postback of a button.

I also continue to call the postback event recursively in order to maintain a reverse ajax session.

Here is my code:  (Although this is a dll, I tried the same code in the web page.  With the same result.)

{
    public class Wait
    {
        public int NumberOfSeconds { get; private set; }

        public Wait()
        {
            NumberOfSeconds = DEFAULT_NUMBER_OF_SECONDS;
        }

        public Wait(int iNumberOfSeconds)
        {
            NumberOfSeconds = iNumberOfSeconds;
        }

        public void StartWait()
        {
            backgroundWorker1 = new BackgroundWorker();
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
            backgroundWorker1.RunWorkerAsync();
        }

        public void StopWait()
        {
            if (backgroundWorker1.WorkerSupportsCancellation == true)
            {
                // Cancel the asynchronous operation.
                backgroundWorker1.CancelAsync();
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(NumberOfSeconds * 1000); //in milliseconds
            throw new Exception("DialogWait: timer has elapsed."); //this causes program termination from an uncaught exception
        }
//this method is never called
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == false)
            {
                throw new Exception("DialogWait: timer has elapsed.");
            }
        }

        /// <summary>
        /// background worker
        /// </summary>
        private BackgroundWorker backgroundWorker1 = new BackgroundWorker();

        /// <summary>
        /// default timeout (in seconds)
        /// </summary>
        private const int DEFAULT_NUMBER_OF_SECONDS = 30; 
    }
}

The exception in the DoWork method terminates the program due to an uncaught exception.  The RunWorkCompleted event handler is never called.  (Even without the exception thrown.)

Any suggestions?


Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles



Latest Images