如何防止线程抛出异常? [英] how to prevent the thread throw exception?

查看:77
本文介绍了如何防止线程抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
                }
            }
        }
    }
}



当我第一次点击按钮时,线程会运行正常和控制台输出i ++值,当我第二次单击按钮时,线程将抛出异常。

我怎样才能立即重启线程并防止线程抛出异常?


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 ?

推荐答案

哈 - 这个古老的问题。



据我所知,没有办法保证thread.abort()实际上可以阻止线程进程。最好自己停止线程进程并等待线程退出。



这样的事情(这就是我的工作原理):





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):


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();
        }
    }
}







查看此文章其中包括以下一些原则:

初学者在.NET中的线程指南:n的第3部分 [ ^ ]



希望有所帮助^ _ ^

Andy




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


这篇关于如何防止线程抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆