Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ThreadTest
{
    public partial class Form1 : Form
    {
        Thread thTest;

        public Form1()
        {
            InitializeComponent();
            thTest = new Thread(new ParameterizedThreadStart(ThreadTest));
        }

        int i = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            thTest.Abort();
            thTest = new Thread(new ParameterizedThreadStart(ThreadTest));

            i = 0;
            thTest.Start();
        }

        void ThreadTest(object oState)
        {
            while (true)
            {
                try
                {
                    Console.WriteLine((i++).ToString());
                    System.Threading.Thread.Sleep(0);
                }
                catch(Exception e)
                {
                    throw e;
                }
            }
        }
    }
}

when i first click the button,thread will run normal and the console output i++ value,when i second click the button ,the thread will throw exception.
how can i can immediately restart the thread and also prevent the thread throw exception ?
Posted
Comments
CPallini 24-Aug-15 4:59am    
That's bad design, you should gracefully terminate the thread.

1 solution

Ha - this age old issue.

As far as I can tell, there is no way to guarantee that a thread.abort() will ever actually be able to stop the thread process. It is much better to stop the threaded process yourself and wait for the thread to exit.

Something like this (this is how I do it atm):


C#
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ThreadTest
{
    public partial class Form1 : Form
    {
        Thread thTest;

        public Form1()
        {
            thTest = new Thread(new ParameterizedThreadStart(ThreadTest));
        }

        int i = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            //tell process to stop
            _cancelThread = true;

            //check process is running.  If so then wait
            if(_busy)
                _resetEvent.WaitOne();

            //start the new process
            thTest = new Thread(new ParameterizedThreadStart(ThreadTest));
            //reset the wait flag
            _resetEvent = new ManualResetEvent(false);

            i = 0;
            thTest.Start();
        }

        ManualResetEvent _resetEvent;  //Used to halt the calling thread until ThreadTest has finished
        bool _cancelThread; //Used to ask ThreadTest to stop
        bool _busy;  //Used to tell calling thread that ThreadTest is running

        void ThreadTest(object oState)
        {
            _busy = true;
            _cancelThread = false;
            while (!_cancelThread)
            {
                try
                {
                    Console.WriteLine((i++).ToString());
                    System.Threading.Thread.Sleep(0);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            _busy = false;
            _resetEvent.Set();
        }
    }
}



Check out this article which goes over some of these principles:
Beginner's Guide to Threading in .NET: Part 3 of n[^]

Hope that helps ^_^
Andy
 
Share this answer
 
Comments
CPallini 24-Aug-15 4:58am    
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