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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:25:29+00:00 2026-06-11T18:25:29+00:00

I have a thread started and I want the user to be able to

  • 0

I have a thread started and I want the user to be able to interrupt it by clicking a button on the form. I found the following code and it demonstrates what I want nicely.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace ExThread {

public partial class MainForm : Form {
    public int clock_seconds=0;

    [STAThread]
    public static void Main(string[] args) {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }

    public MainForm() {
        InitializeComponent();

        Thread thread_clock = new Thread(new ThreadStart(Thread_Clock));
        thread_clock.IsBackground = true;
        thread_clock.Start();
    }

    delegate void StringParameterDelegate (string value);

    public void Update_Label_Seconds(string value) {
        if (InvokeRequired) {
            BeginInvoke(new StringParameterDelegate(Update_Label_Seconds), new object[]{value});
            return;
        }
        label_seconds.Text= value + " seconds";
    }       

    void Thread_Clock() {
        while(true) {
            clock_seconds +=1;
            Update_Label_Seconds(clock_seconds.ToString());
            Thread.Sleep(1000);
        }
    }

    private void btnStop_Click(object sender, EventArgs e)
    {

    }
}
}

I have added the btnStop method. What code needs to be added to stop the thread_clock thread.

Thanks.

  • 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-11T18:25:30+00:00Added an answer on June 11, 2026 at 6:25 pm

    First, the thread needs to be able to recognize that it should end. Change

    void Thread_Clock() {
        while(true) {
    

    to

    bool endRequested = false;
    void Thread_Clock() {
        while(!endRequested) {
    

    And then set endRequested to True in your button click handler.

    private void btnStop_Click(object sender, EventArgs e)
    {
        endRequested = true;
    }
    

    Note that for this specific case, it is probably more appropriate to use a Timer

    http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

    Simply start and stop the timer as desired. You would update the clock from the timer’s Tick() event.

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

Sidebar

Related Questions

I have a local thread that is started on button-click event from the main
I have a Thread which runs my game loop. I want to be able
Let's say I started a thread and I have something like this: ...//initiate all
I have thread where downloading xml file a this xml file i want to
I have several thread pools and I want my application to handle a cancel
I have a thread that sits in a blocking recv() loop and I want
I have this code: import thread def print_out(m1, m2): print m1 print m2 print
I have an application which spawns a new thread when a user asks for
Is there any way I can have thread-safe coroutines along with boost::asio? I want
I have a class function that I want to start up as a thread.

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.