Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8418985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:28:54+00:00 2026-06-10T02:28:54+00:00

So I have a webpage scraper that uses backgroundworker to process each page. I

  • 0

So I have a webpage scraper that uses backgroundworker to process each page. I also want to mention that I’m using MVVM light framework.

Inside my MainViewModel Constructor I am initializing the backgroundworker:

backgroundWorker = new BackgroundWorker()
        {
            WorkerReportsProgress = true,
            WorkerSupportsCancellation = true
        };
        backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);

On the LoadCompleted event of a WebBrowser control I start the backgroundworker:

wb = sender; //sender is the webbrowser control
        if (!backgroundWorker.IsBusy)
        {
            backgroundWorker.RunWorkerAsync();
        }

My next two methods are DoWork And StopWork:

private System.Threading.AutoResetEvent _resetEvent = new System.Threading.AutoResetEvent(false);
    private object wb;

    void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker wk = sender as BackgroundWorker;

        if (wb != null)
        {
            FetchPage(wb);

            if (wk.CancellationPending)
            {
                MessageBox.Show("Cancellation pending!");
            }

            _resetEvent.Set();
        }
    }

    private void StopWork(object sender)
    {
        backgroundWorker.CancelAsync();
        _resetEvent.WaitOne();
    }

The fetchpage method will grab the sourcecode of the webbrowser control and start parsing it for content.

Inside of FetchPage I’m using BeginInvoke to update my UI thread:

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(
            () =>
            { ... }

My issue:
When I hit the Cancel button the StopWork method get’s invoked, the cancel property on the backgroundWorker is set correctly to true, but the app just keeps going on. My if (wk.CancellationPending) is always false.

Any idea on what am I doing wrong here? I looked at tons of examples online and here on StackOverflow and they all state the same things that i already done.

Thanks.

EDIT:

After Ernos reply I tried passing the CancellationPending property to the FetchPage method and check for it in different locations, but it did not stop the processing.

void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker wk = sender as BackgroundWorker;

        if (wb != null)
        {
            FetchPage(wb, wk.CancellationPending);

            _resetEvent.Set();
        }
    }

Inside of FetchPage I’m using BeginInvoke to update my UI thread:

private void FetchPage(object sender, bool stopAll)
{
     if (stopAll)
     {
         return;
     }
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(
            () =>
            { ... }

What I have tried and worked was:

private bool stopAllWork = false;

...

private void StopWork(object sender)
    {
        stopAllWork = true;
        backgroundWorker.CancelAsync();
        _resetEvent.WaitOne();
    }

and then inside DoWork:

void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker wk = sender as BackgroundWorker;

        if (wb != null)
        {
            FetchPage(wb, stopAllWork);

            _resetEvent.Set();
        }
    }

Now, because of this implementation my concern is if there will be any rogue backgroundWorkers remaining?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:28:56+00:00Added an answer on June 10, 2026 at 2:28 am

    You need to evaluate the CancellationPending inside the FetchPage method.

    You are checking it AFTER the load of work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nodejs script that uses phantomjs-node to scrape a webpage. It works
I have a PHP/jQuery scraper function that essentially looks for web page content. One
I have this webpage that I want to post to my profile in FB.
I have a webpage that uses JavaScript to retrieve JSON from a web service.
I have a webpage that is using Google's custom search engine. I wanted to
I have a webpage that uses the special character &#xFE3E in the HTML. In
I have a scraper that scans a webpage for specific pieces of information and
I have a webpage that is transferring A stylesheet as a text/plain MIME type.
I have a webpage that when landed on, will detect a user's physical location
I have a webpage that retrieves data (via ajax/php) and shows it in an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.