Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Form have 4 button and a lable

form freezes because of
_thread.Join();
when I click btnStart.
I do not know anything about threading . I wrote this code by seeing some samples.
i need somthing like this to do , start, pause, continue, terminate, main thread wait.
thank you in advance for any idea.




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 test_threading
{
    public partial class Form1 : Form
    {
        public Thread _thread ;
        private static ManualResetEvent _mre = new ManualResetEvent(false);
        public bool _canceled = false;

        public Form1()
        {
            InitializeComponent();
        }

        
        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false; btnPause.Enabled = true; 
            btnContinue.Enabled = false; btnTerminate.Enabled = true;
            _canceled = false;
            _thread = new Thread(DoWork);
            _thread.Start();
            _mre.Set();

            _thread.Join();
            label1.Text = "thread joined";
            btnStart.Enabled = true; btnPause.Enabled = false;
            btnContinue.Enabled = false; btnTerminate.Enabled = false;
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            btnPause.Enabled = false; btnContinue.Enabled = true;
            _mre.Reset();
        }

        private void btnContinue_Click(object sender, EventArgs e)
        {
            btnContinue.Enabled = false; btnPause.Enabled = true; 
            _mre.Set();
        }

        private void btnTerminate_Click(object sender, EventArgs e)
        {
            try
            {
                _thread.Abort();
                _canceled = true;
                btnStart.Enabled = true; btnPause.Enabled = false;
                btnContinue.Enabled = false; btnTerminate.Enabled = false;
            }
            catch(Exception ex)
            { MessageBox.Show("btnTerminate_Click\n" + ex.Message); } 
           
        }
        private void DoWork()
        {
            try 
            {
                for (int cnt = 0; cnt<10000; cnt++)
                {
                    _mre.WaitOne();
                    Count("label1", cnt);
                }
            }
            catch (Exception ex) 
            {
                if (ex.Message!="Thread was being aborted.")
                { MessageBox.Show("DoWork\n" + ex.Message);} 
            }
           
        }
        private delegate void Dstrint(string _controlName, int _cnt);
        public void Count(string _controlName, int _cnt)
        {
            if (Controls[_controlName].InvokeRequired)
            {
                Dstrint h = new Dstrint(Count);
                this.Invoke(h, new object[] { _controlName, _cnt });
            }
            else
            {
                Controls[_controlName].Text = _cnt.ToString();
            }
        }
    }
}
Posted
Updated 19-Oct-14 12:12pm
v3
Comments
BillWoodruff 19-Oct-14 18:53pm    
I am curious: why would you want to stop, or pause, a main thread ?

1 solution

Not a good idea. If you stop or pause the "main thread" your application UI becomes unresponsive.
 
Share this answer
 

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