Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
I want to continuously display some numbers in textbox on click of Start button and stop displaying numbers on Stop button click. I am trying some code using thread. But my Stop button event occurs only after completion of Start button event. I want to stop or interrupt Start button execution in middle. How can i do it using threads. Please Help me.... Thanks....

What I have tried:

I have tried below code-

C#
public partial class Form1 : Form
    {
        Thread threadObj;
        public delegate void delename();
        
        public bool stopProcess = false;
 
        public Form1()
        {
            InitializeComponent();          
        }

        private void Start_Btn_Click(object sender, EventArgs e)
        {
            this.stopProcess = false;
            threadObj = new Thread(new ThreadStart(ThreadStartNow));
            threadObj.Start();
        }

        private void Stop_Btn_Click(object sender, EventArgs e)
        {
            this.stopProcess = true;             
        }
        
        public void ThreadStartNow()
        {
            if (!this.stopProcess)
            {
                this.BeginInvoke(new delename(Method1));
            }
            else
            {
                this.threadObj.Abort();
            }
        }
        public void Method1()
        {
            
                    for (int i = 0; i <= 50; i++)
                    {
                        textBox1.Text = i.ToString();
                        Thread.Sleep(500);
                        textBox1.Refresh();
                    }
  
        }

    }
Posted
Updated 31-Jul-16 18:23pm
v2
Comments
Kenneth Haugland 1-Aug-16 0:23am    
I think it would serve you better to start a Task instead:
https://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx

1 solution

For the 'simple' sort of thing you appear to want to do, have you thought of using a BackgroundWorker ? with BackgroundWorker, you can signal it to be cancelled, and check 'CancellationPending' in the processing loop .. obviously you can put this together yourself, but 'bgw' sets it all up nicely and there are oodles of examples existing

[edit] as Kenneth points out below by including the link to an article, Task.Run() is 'the new BackgroundWorker' .. Task.Run vs BackgroundWorker: Conclusion[^] [/edit]
 
Share this answer
 
v3
Comments
Kenneth Haugland 1-Aug-16 0:38am    
I would think that it would be better to just use Task.Run method instead of a BackgroudnWorker.
http://blog.stephencleary.com/2013/09/taskrun-vs-backgroundworker-conclusion.html
Garth J Lancaster 1-Aug-16 0:45am    
interesting, thanks - I'll read and update my knowledge - I guess what I was saying was 'use something that exists rather than roll your own unless you really need to' .. Task.Run() does seem appropriate as well .. updating answer
BillWoodruff 1-Aug-16 2:44am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900