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 652441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:16:22+00:00 2026-05-13T22:16:22+00:00

I am building a proof of concept application before it gets rollout to the

  • 0

I am building a proof of concept application before it gets rollout to the real one.

Scenario
I should be able to stop processing in the middle of it.

Toolbar 2 buttons “Start” & “Stop”

User press start and it process a long running task.
User decides out of the blue to stop the task.

I cannot seem to get threading right!! I cannot press stop as it’s waiting for the long running task as if the long running task is actually running on UI thread and not as intented on background thread.

What Am I doing wrong can you spot it? Thanks for your help

public partial class TestView : UserControl
{

    private readonly BackgroundWorker _worker;


    public TestView 
    {
        InitializeComponent(); 
        _worker = new BackgroundWorker();
        _worker.RunWorkerCompleted += RunWorkerCompleted;
        _worker.DoWork+=DoWork;
        _worker.WorkerReportsProgress = true;
        _worker.ProgressChanged+=_worker_ProgressChanged;
        _worker.WorkerSupportsCancellation = true;
    }


    static void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled)
        {
            MessageBox.Show("The task has been cancelled");
        }
        else if (e.Error != null)
        {
            MessageBox.Show("Error. Details: " + e.Error);
        }
        else
        {
            MessageBox.Show("The task has been completed. Results: " + e.Result);
        }
    }
    private delegate void SimpleDelegate();
    void DoWork(object sender, DoWorkEventArgs e)
    {
        for (var i = 0; i < 1000000; i++)
        {
            _worker.ReportProgress(i, DateTime.Now); 
           // SimpleDelegate simpleDelegate = () => txtResult.AppendText("Test" + System.Environment.NewLine);

            //Dispatcher.BeginInvoke(DispatcherPriority.Normal, simpleDelegate);
        }

        MessageBox.Show("I have done it all");
    }
    private void _worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        DateTime time = Convert.ToDateTime(e.UserState); 
        txtResult.AppendText(time.ToLongTimeString());
        txtResult.AppendText(Environment.NewLine);
    }

    private void BtnStart_Click(object sender, RoutedEventArgs e)
    {
        _worker.RunWorkerAsync();
    }

    private void BtnStop_Click(object sender, RoutedEventArgs e)
    {
        _worker.CancelAsync();
        MessageBox.Show("Process has been stopped!");
    }
}
  • 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-05-13T22:16:22+00:00Added an answer on May 13, 2026 at 10:16 pm

    You run a very tight loop inside of DoWork and continuously push Invoked ProgressUpdates to the Main Thread. That will make it sluggish.

    But the real problem is that DoWork has to cooperate in Cancellation:

    void DoWork(object sender, DoWorkEventArgs e)
    {       
       for (var i = 0; i < 1000000; i++)
       {
          if (_worker.CancelationPending)
          {
             e.Cancel = true;
             break;  // or: return to skip the messagebox
          }
          _worker.ReportProgress(i, DateTime.Now); 
       }
    
       MessageBox.Show("I have done it all");  // remove or make depend on Cancelled
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a proof of concept for using Apache Camel. I am trying
I'm building a simple proof-of-concept for an iPad-specific website that would use video transitions
If I'm developing a proof-of-concept application, does it make sense to invest time in
I'm building a simple app with Knockout.js as a proof of concept. As I'm
I'm trying to get a proof-of-concept PivotViewer application up and running, but I can't
Total beginner in WPF so bear with me. I building a proof of concept
Building a proof-of-concept web site for a group project. Regarding usernames we would like
As a proof of concept, I'm building this extremely simple Twitter Friends crawler. Here's
I'm building a LINQ-based query generator. One of the features is being able to
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't

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.