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

  • Home
  • SEARCH
  • 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 6856563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:54:08+00:00 2026-05-27T01:54:08+00:00

I’m new to C# and object-oriented programming in general. I’ve been trying to implement

  • 0

I’m new to C# and object-oriented programming in general. I’ve been trying to implement a “Cancel” button into my GUI so that the user can stop it mid-process.

I read this question: How to implement a Stop/Cancel button? and determined that a backgroundWorker should be a good option for me, but the example given doesn’t explain how to hand arguments to the backgroundWorker.

My problem is that I do not know how to pass an argument into backgroundWorker such that it will stop the process; I have only been able to get backgroundWorker to stop itself.

I created the following code to try to learn this, where my form has two buttons (buttonStart and buttonStop) and a backgroundWorker (backgroundWorkerStopCheck):

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Timers;

namespace TestBackgroundWorker
{
    public partial class Form1 : Form
    {
        public Form1()
        {         
            InitializeComponent();

            // Set the background worker to allow the user to stop the process. 
            backgroundWorkerStopCheck.WorkerSupportsCancellation = true;
        }

        private System.Timers.Timer myTimer;

        private void backgroundWorkerStopCheck_DoWork(object sender, DoWorkEventArgs e)
        {
            //If cancellation is pending, cancel work.  
            if (backgroundWorkerStopCheck.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            // Notify the backgroundWorker that the process is starting.
            backgroundWorkerStopCheck.RunWorkerAsync();
            LaunchCode();
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            // Tell the backgroundWorker to stop process.
            backgroundWorkerStopCheck.CancelAsync();
        }

        private void LaunchCode()
        {
            buttonStart.Enabled = false; // Disable the start button to show that the process is ongoing.
            myTimer = new System.Timers.Timer(5000); // Waste five seconds.
            myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
            myTimer.Enabled = true; // Start the timer.
        }

        void myTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            buttonStart.Enabled = true; // ReEnable the Start button to show that the process either finished or was cancelled.
        }
    }
}

The code, if it worked properly, would just sit there for five seconds after the user clicked “Start” before re-enabling the Start button, or would quickly reactivate the Start button if the user clicked “Stop”.

There are two problems with this code that I am not sure how to handle:

1) The “myTimer_Elapsed” method results in an InvalidOperationException when it attempts to enable the Start button, because the “cross-thread operation was not valid”. How do I avoid cross-thread operations?

2) Right now the backgroundWorker doesn’t accomplish anything because I don’t know how to feed arguments to it such that, when it is canceled, it will stop the timer.

I’d appreciate any assistance!

  • 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-27T01:54:09+00:00Added an answer on May 27, 2026 at 1:54 am

    First of all, the problem to avoid “cross-thread operation was not valid” is use Invoke on controls. You cannot use a control from a different thread.

    About the second issue, I would implement it in the following way. This is a minimum background worker implementation with cancel support.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                // Set the background worker to allow the user to stop the process. 
                backgroundWorkerStopCheck.WorkerSupportsCancellation = true;
                backgroundWorkerStopCheck.DoWork += new DoWorkEventHandler(backgroundWorkerStopCheck_DoWork);
            }
    
            private void backgroundWorkerStopCheck_DoWork(object sender, DoWorkEventArgs e)
            {
                try
                {
                    for (int i = 0; i < 50; i++)
                    {
                        if (backgroundWorkerStopCheck.CancellationPending)
                        {
                            // user cancel request
                            e.Cancel = true;
                            return;
                        }
    
                        System.Threading.Thread.Sleep(100);
                    }
                }
                finally
                {
                    InvokeEnableStartButton();
                }
            }
    
            private void buttonStart_Click(object sender, EventArgs e)
            {
                //disable start button before launch work
                buttonStart.Enabled = false;
    
                // start worker
                backgroundWorkerStopCheck.RunWorkerAsync();
            }
    
            private void buttonStop_Click(object sender, EventArgs e)
            {
                // Tell the backgroundWorker to stop process.
                backgroundWorkerStopCheck.CancelAsync();
            }
    
            private void InvokeEnableStartButton()
            {
                // this method is called from a thread,
                // we need to Invoke to avoid "cross thread exception"
                if (this.InvokeRequired)
                {
                    this.Invoke(new EnableStartButtonDelegate(EnableStartButton));
                }
                else
                {
                    EnableStartButton();
                }
            }
    
            private void EnableStartButton()
            {
                buttonStart.Enabled = true;
            }
        }
    
        internal delegate void EnableStartButtonDelegate();
    }
    

    About passing arguments to the worker, you can pass any object in the RunWorkerAsync() method, and its reveived in the backgroundWorkerStopCheck_DoWork method:

      ...
      backgroundWorkerStopCheck.RunWorkerAsync("hello");
      ...
    
      private void backgroundWorkerStopCheck_DoWork(object sender, DoWorkEventArgs e)
      {
          string argument = e.Argument as string;
          // argument value is "hello"
          ...
      }
    

    Hope it helps.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,

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.