0

I'm trying to start thread and then end thread by clicking buttons in Winform but the MessageBox does not appear after clicking the Stop button( which is the button 3 here). \

Here's my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace ThreadForm
{
    public partial class Form1 : Form
    {
        public static volatile bool isPausePressed = false;
        Thread SequenceTest = new Thread(DoWork);
        Thread Seq1Test = new Thread(TestSequence1);
        Thread Seq2Test = new Thread(TestSequence2);
        Thread Seq3Test = new Thread(TestSequence3);
        

        public Form1()
        {
            InitializeComponent();
        }
        Form2 objform;

        private void button1_Click(object sender, EventArgs e)
        {
            objform = new Form2();
            objform.Show();
            var formWithButton = new Form2();
            formWithButton.Btn_Change.BackColor = Color.White;
            SequenceTest.Start();
            Seq1Test.Start();
            Seq2Test.Start();
            Seq3Test.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            isPausePressed = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bool a, b, c, d;
            a = b = c = d = false;
            SequenceTest.Join();
            /*if (SequenceTest.ThreadState == System.Threading.ThreadState.Stopped)
                a = true;*/
            Seq1Test.Join();
            /*if (Seq1Test.ThreadState == System.Threading.ThreadState.Stopped)
                b = true;*/
            Seq2Test.Join();
            /*if (Seq2Test.ThreadState == System.Threading.ThreadState.Stopped)
                c = true;*/
            Seq3Test.Join();
            /*if (Seq3Test.ThreadState == System.Threading.ThreadState.Stopped)
                d = true;*/
            MessageBox.Show("Done!", "ThreadTest");
        }

        public static void DoWork()
        {
            Thread.Sleep(1000);
            //Console.WriteLine("Current main task working on thread: {0}", Thread.CurrentThread.ManagedThreadId);
            while (!isPausePressed)
            {
                Thread.Sleep(100);
            }
        }

        public static void TestSequence1()
        {
            int i = 0;
            Thread.Sleep(1000);
            //Console.WriteLine("Sequence1 working on thread: {0}", Thread.CurrentThread.ManagedThreadId);
            while (true)
            {
                i++;
                //Console.WriteLine("Current i value: {0}", i);
            }
        }

        public static void TestSequence2()
        {
            int j = 0;
            //Console.WriteLine("Sequence2 working on thread: {0}", Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(1000);
            while (true)
            {
                j++;
                //Console.WriteLine("Current j value: {0}", j);
            }
        }

        public static void TestSequence3()
        {
            int k = 0;
            Thread.Sleep(1000);
            //Console.WriteLine("Sequence3 working on thread: {0}", Thread.CurrentThread.ManagedThreadId);
            while (true)
            {
                k++;
                //Console.WriteLine("Current k value: {0}", k);
            }
        }
    }
}

Thanish K
  • 37
  • 5
  • [`Thread.Join()`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.join): _"Blocks the calling thread **until** the thread represented by this instance terminates."_ There's nothing in your code that is terminating `Seq1Test`, `Seq2Test`, or `Seq3Test`. Only `SequenceTest` is terminated (because of the `!isPausePressed` check). – 41686d6564 stands w. Palestine Apr 19 '21 at 06:41
  • What is it you are actually trying to do? You would not normally use threads directly in modern programs, in favor of tasks and async/await. Using a cancellation token to cancel a task, or changing a timer to stop a reoccurring task would probably be more appropriate. But it is difficult to tell if you do not state the purpose of your question. – JonasH Apr 19 '21 at 06:47
  • @41686d6564 Ok got it! It works now thank you – Thanish K Apr 19 '21 at 06:53
  • @JonasH I'm trying to learn multithreading so that I can use it on automation/machine where I can use one thread for each sequence without using timer to start or stop the sequence. – Thanish K Apr 19 '21 at 06:55
  • @ThanishK I'm glad I helped. That said, if you don't have a specific reason for using threads, you should consider using JonasH's advice and use tasks with CancellationTokens instead. – 41686d6564 stands w. Palestine Apr 19 '21 at 06:56
  • So what is a sequence? Multithreading makes things *more* complex, not less. So use it only when you have to. And if you use it, you should spend a fair amount time reading up on best practices and potential hazards *before* you start. Otherwise you are likely to introduce bugs that are difficult to reproduce. – JonasH Apr 19 '21 at 08:50

0 Answers0