BackgroundWorker error: The name 'e' does not exist in the current context

Category: c# ide

Question

RayvenDad on Tue, 17 Jul 2012 15:46:51


I'm having a little issue with multi-threading in C# 2010 and I'm hoping one of you guru's can point me in the right direction:

I writing a application using WPF.  I've got 95% of it working but have stumbled one one small part.  I use BackgroundWorker to run a method in an external class.

Here's the definition of my BackgroundWorker object:

            m_oWorker = new BackgroundWorker();
            m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
            m_oWorker.ProgressChanged += new ProgressChangedEventHandler(m_oWorker_ProgressChanged);
            m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_oWorker_RunWorkerCompleted);
            m_oWorker.WorkerReportsProgress = true;
            m_oWorker.WorkerSupportsCancellation = true;

I have a button defined that calls m_oWorker.RunWorkerAsync()

Here's my m_oWorker_DoWork code:

        public void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Bugs = new List<Bug>();
            RTCOPS = new List<string>();
            Bugs.Clear();
            RTCOPS.Clear();
            metrics = new int[6][];
            PSQuery psq = new PSQuery();
            psq.RunQuery();

            m_oWorker.ReportProgress(100, "Calculations completed, launching email");
            System.Threading.Thread.Sleep(500);

            CreateMailItem();

        }

PSQuery is a method existing in a class file.  Everything workes smoothly save the cancellation part.  When I attempt to set the e.Cancel flag so that the thread can be terminated, I get the following error: The name 'e' does not exist in the current context

Here's the code I'm using:

        public void RunQuery()
        {
            MainWindow.m_oWorker.ReportProgress(0, "Opening connection to Product Studio database....");
            if (MainWindow.m_oWorker.CancellationPending)
            {
                // Set the e.Cancel flag so that the WorkerCompleted event
                // knows that the process was cancelled.
                e.Cancel = true;
                MainWindow.m_oWorker.ReportProgress(0);
                return;
            }

And the error is shown at "e.Cancel = true;

Any ideas or pointers?  thanks in advance!

Replies

Reed Copsey, Jr on Tue, 17 Jul 2012 16:06:20


You'd need to change your RunQuery method to accept the DoWorkEventArgs parameter:

public void RunQuery(DoWorkEventArgs e)
{
  // Now you can set e.Cancel here
}

In your background worker event hanlder, you'd now call this as RunQuery(e);

RayvenDad on Tue, 17 Jul 2012 16:12:06


Thank you so much for the quick response but I get the following error now after doing that:

        public void RunQuery(DoWorkEventArgs e)
        {
            MainWindow.m_oWorker.ReportProgress(0, "Opening connection to Product Studio database....");
            if (MainWindow.m_oWorker.CancellationPending)
            {
                // Set the e.Cancel flag so that the WorkerCompleted event
                // knows that the process was cancelled.
                e.Cancel = true;
                MainWindow.m_oWorker.ReportProgress(0);
                return;
            }

The type or namespace name 'DoWorkEventArgs; could not be found (are you missing a directive or an assembly reference?)

The above code is in my Class file (PSQuery.cs) and not in the MainWindow file (MainWindow.xaml.cs)

Reed Copsey, Jr on Tue, 17 Jul 2012 16:22:55


At the top of your file (PSQuery.cs), add:

    using System.ComponentModel;

 

RayvenDad on Tue, 17 Jul 2012 16:45:43


One last error (I hope!).  Everything is working now, save the actual call to RunQuery from MainWindow.xaml.cs:

            PSQuery psq = new PSQuery();
            psq.RunQuery();

I get the following error:

void PSQuery.RunQuery(DoWorkArgs e)

Error:

No overload for method 'RunQuery' takes 0 arguements.

Reed Copsey, Jr on Tue, 17 Jul 2012 16:48:04


See the last sentence of my first post ;)

"In your background worker event handler, you'd now call this as RunQuery(e);"

You need to change that to:

  psq.RunQuery(e);

RayvenDad on Tue, 17 Jul 2012 16:50:47


I'm so sorry I didn't fully read your earlier response and played the noobie, running off to implement the fix.

Thank you so much for your speedy response and the fix for this.