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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:10:01+00:00 2026-05-26T00:10:01+00:00

How to kill one button’s event when a new button is clicked. I have

  • 0

How to kill one button’s event when a new button is clicked.

I have one event (Button G) running.(has a while loop waiting for some input).
I have a another button for quit operation.
Now. I cannot click any other button when button G’s event is running.
How can I solve that?
Thanks

Hi, @Grokodile thank you for your code. So I commented your code here, Should I put my job logic code where I commented below? Thans

    using System;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Threading;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private BackgroundWorker _worker;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void RunButtonClickHandler(object sender, RoutedEventArgs e)
        {
            _worker = new BackgroundWorker {WorkerSupportsCancellation = true};
            _worker.DoWork += BackgroundWorkerTask;
            _worker.RunWorkerAsync();
       //I should Put my job logic here, right?
        }

        private void StopButtonClickHandler(object sender, RoutedEventArgs e)
        {
            if (_worker != null && _worker.IsBusy) _worker.CancelAsync();
             //I should Put my job logic here, right?

        }

        private void BackgroundWorkerTask(object sender, DoWorkEventArgs e)
        {
            // this runs on the BackgroundWorker thread.

            while (_worker.CancellationPending == false)
            {
                Thread.Sleep(500);
                //  You have to use the Dispatcher to transfer the effects of
                //  work done in the worker thread back onto the UI thread.
                Dispatcher.BeginInvoke(new Action(UpdateTime), DispatcherPriority.Normal, null);
            }
        }

        private void UpdateTime()
        {
            // Dispatcher runs this on the UI thread.

            timeTextBlock.Text = DateTime.Now.ToString();
        }
    }
}
  • 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-26T00:10:01+00:00Added an answer on May 26, 2026 at 12:10 am

    Futher to what H.B. and dowhilefor have said, here is a sample that shows starting a task on a background thread using BackgroundWorker with one Button and ending it with another Button, note the use of Dispatcher.BeginInvoke:

    XAML

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow"
            Height="80"
            Width="640"
            FontSize="16">
        <DockPanel VerticalAlignment="Center">
            <Button Margin="10,0"
                    x:Name="runButton"
                    DockPanel.Dock="Left"
                    Click="RunButtonClickHandler">Run</Button>
            <Button Margin="10,0"
                    x:Name="stopButton"
                    DockPanel.Dock="Left"
                    Click="StopButtonClickHandler">Stop</Button>
            <TextBlock Margin="10,0">The Time Is Now:</TextBlock>
            <TextBlock x:Name="timeTextBlock"
                       Margin="10,0" />
        </DockPanel>
    </Window>
    

    Code Behind

    using System;
    using System.ComponentModel;
    using System.Threading;
    using System.Windows;
    using System.Windows.Threading;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private BackgroundWorker _worker;
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void RunButtonClickHandler(object sender, RoutedEventArgs e)
            {
                _worker = new BackgroundWorker {WorkerSupportsCancellation = true};
                _worker.DoWork += BackgroundWorkerTask;
                _worker.RunWorkerAsync();
            }
    
            private void StopButtonClickHandler(object sender, RoutedEventArgs e)
            {
                if (_worker != null && _worker.IsBusy) _worker.CancelAsync();
            }
    
            private void BackgroundWorkerTask(object sender, DoWorkEventArgs e)
            {
                // this runs on the BackgroundWorker thread.
    
                while (_worker.CancellationPending == false)
                {
                    Thread.Sleep(500);
                    //  You have to use the Dispatcher to transfer the effects of
                    //  work done in the worker thread back onto the UI thread.
                    Dispatcher.BeginInvoke(new Action(UpdateTime), DispatcherPriority.Normal, null);
                }
            }
    
            private void UpdateTime()
            {
                // Dispatcher runs this on the UI thread.
    
                timeTextBlock.Text = DateTime.Now.ToString();
            }
        }
    }
    

    EDIT – A Little More Explanation

    RunButtonClickHandler

    Creates and initializes a BackgroundWorker so that it supports cancellation.
    Attaches a DoWorkEventHandler to the DoWork event, i.e. BackgroundWorkerTask.
    Starts excecution of the background operation with the call to RunWorkerAsync, i.e. creates a new thread (actually it uses a thread from the thread pool) and runs the code in BackgroundWorkerTask on that thread.

    BackgroundWorkerTask

    If you want to do work that would otherwise cause the UI to freeze when running on the main UI thread (e.g. search for undiscovered prime numbers) then you do it here in the background thread.

    UpdateTime

    All WPF Controls inherit from DispatcherObject and are associated with a Dispatcher which manages the execution of work done in the UI’s single thread. If you need to do work such as setting the text of a TextBlock, you can’t do it from the background thread, trying to do so will cause an exception to be thrown. UpdateTime is queued back onto the UI thread by the Dispatcher when Dispatcher.BeginInvoke is called from BackgroundWorkerTask. This way you can get the results of work done in the background before cancelling the background threads execution.

    StopButtonClickHandler

    Changes _worker.CancellationPending to true with the call to CancelAsync causing the while loop to exit and thus for execution to leave the BackgroundWorkerTask event handler.

    In short, you can do work in two places, either in BackgroundWorkerTask or in UpdateTime, but you can only carry out changes to UI elements from UpdateTime.

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

Sidebar

Related Questions

When you pipe two process and kill the one at the output of the
Is it possible kill the thread of a BackgroundWorker ? In my DoWork event,
I have a number of Gearman workers running constantly, saving things like records of
I'm completely new to Linux and have been trying to get my (Windows built)
Suppose I have a process which spawns exactly one child process. Now when the
I have a VirtualBox process hanging around which I tried to kill ( KILL
I have a service variable that is initialized : service = new google.gdata.calendar.CalendarService('timeless'); This
Hi i made an application which has 2 views one paging scroll view which
Trying to kill two birds with one stone. I'm not well versed in certificates,
I am shelling out to do some work and one of the requirements is

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.