`
mlzboy
  • 浏览: 739711 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Thread is suspended; attempting to abort的错误处理

阅读更多

在自制线程池的过程中遇到这样一个情景:需要中断一个可能是正在执行的任务,这个任务也可能任务队列中,由于是线程池,任务执行完后线程不是消失的而是继续等待接收下一个任务的,我使用的是Thread.Suspend来暂停线程当线程完成一项任务后,这时候如果使用Thread.Abort来中断这个ThreadState为Suspended的线程是会报错的。

以下代码重现了我的错误

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadPoolTest
{
    
class Class5
    {
        
static void Main(string[] args)
        {
            Thread thd
=new Thread(new ThreadStart(test));
            thd.Start();
            Thread.Sleep(
10000);
            thd.Abort();
            Console.WriteLine();
            Console.ReadLine();
        }

        
private static void test()
        {
            Console.WriteLine(
"aa");
            Thread.CurrentThread.Suspend();
            Console.WriteLine(
"aa");

        }
    }
}

google许久,发现一篇英文http://www.code-magazine.com/article.aspx?quickid=0309071&page=4描述的非常清楚,其中有如下一段:

If Abort() is called before the thread is started, .NET will never start the thread once Thread.Start() is called. If Thread.Abort() is called while the thread is blocked (either by calling Sleep(), or Join(), or if the thread is waiting on one of the .NET synchronization objects), .NET unblocks the thread and throws ThreadAbortException in it. However, you cannot call Abort() on a suspended thread. Doing so will result on the calling side with an exception of type ThreadStateException, with the error message "Thread is suspended; attempting to abort." In addition, .NET will terminate the suspended thread without letting it handle the exception

就是说线程的状态是Suspended的时候,是不能使用Thread.Abort来中止的,那我们的情况怎么处理呢,我们换了一个思路,既然线程的状态已经是 Suspended了为什么我们还要Abort它呢,将它再次放入线程池为后续任务工作不是更好吗?

于是加一个条件判断

if(Thread.ThreadState==ThreadState.Suspended):
       pass

这样就ok了

分享到:
评论

相关推荐

    C#多线程之Thread中Thread.IsAlive属性用法分析

    - **挂起(Suspended)**:线程被挂起,无法执行,但内存占用仍然存在。 - **终止(Stopped/Terminated)**:线程执行完成或被`Abort()`方法强制停止。 2. **Thread.IsAlive属性** `Thread.IsAlive`属性返回一个...

    A semi-analytical total suspended sediment retrieval model

    A simple semi-analytical model to estimate total suspended sediment matter (3S) was established for estimating TSM concentrations in Changjiang River Estuary. The results indicate that 3S model with ...

    LUA - coroutine

    The concept of a coroutine is one of the...“the execution of a coroutine is suspended as control leaves it, only to carry on where it left off when control re-enters the coroutine at some later stage”.

    施耐德-Medium power busbar trunking Canalis KS(100 to 800A) Catalogue.pdf

    施耐德-Medium power busbar trunking Canalis KS(100 to 800A) Cataloguepdf,施耐德-Medium power busbar trunking Canalis KS(100 to 800A) Catalogue Components for changing direction 1、Edgewise elbow. One ...

    a project model for the FreeBSD Project.7z

    The vision is “To produce the best UNIX-like operating system package possible, with due respect to the original software tools ideology as well as usability, performance and stability.” The ...

    在C#中编写多线程应用程序

    if (thread1.ThreadState == ThreadState.Suspended) { thread1.Resume(); } ``` #### 示例代码解析 下面给出一个简单的多线程示例程序,该程序展示了如何在C#中创建并启动一个新的线程: ```csharp using ...

    C#多线程——关于C#多线程的一个技术文档

    if (thread.ThreadState == ThreadState.Suspended) { thread.Resume(); } ``` 6. **线程示例** 以下是一个简单的多线程示例,它创建了一个新的线程并运行`ThreadProc`方法: ```csharp using System; ...

    Account Suspended(解决方案).md

    Account Suspended(解决方案).md

    2009 达内Unix学习笔记

    集合了 所有的 Unix命令大全 ...telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss ... 各个 shell 可互相切换 ksh:$ sh:$ csh:guangzhou% bash:bash-3.00$ ... 命令和参数之间必需用空格隔...

    C#多线程学习

    - **线程的状态**: 例如 `ThreadState` 属性可以用来检查线程的当前状态,如是否正在运行(`Running`)、是否已被暂停(`Suspended`)等。 - **线程的控制**: `Thread` 类提供了多种方法来控制线程的行为,比如 `...

    ThreadStartStop

    5. **线程终止**:`Thread.Abort()`方法可以强制结束一个线程,但这可能会导致未捕获的异常,因此通常不推荐。更安全的方式是通过设置某个标志,让线程自己检查并优雅地结束。 6. **线程状态管理**:线程有多种状态...

    dotNetFramework多线程编程

    线程结束通常由`Thread.Join`、`Abort`或自然完成工作。`Join`等待线程结束,`Abort`强制终止,但应谨慎使用,因为它可能导致资源泄露和不可预测的行为。 总之,理解并熟练运用.NET Framework的多线程编程技术,能...

    Suspended twin-core fiber for optical switching

    A kind of novel fiber, comprising two fiber cores which are suspended in air inside the outer cladding via a central thin membrane, is proposed for optical switching application. When a hydrostatic ...

    windows 多线程处理

    而线程从运行状态(Running)变为停止状态(Stopped)可能是线程执行完毕或被`Thread.Abort`方法中断。线程在遇到同步锁或等待I/O操作时,会从运行状态转变为阻塞(Blocked)或等待-休眠-联接(WaitSleepJoin)状态...

    CWinForm多线程开发.doc

    7. **ThreadState**:表示线程的状态,如`Aborted`、`Running`、`Stopped`、`Suspended`等,这些状态可以帮助开发者监控和控制线程的行为。 在WinForm应用中,直接在新线程中修改UI组件的属性是不允许的,因为这会...

    将vmware从suspended状态强制转换到power off状态

    本文将详细介绍如何在 VMware 中处理虚拟机从挂起(suspended)状态无法正常恢复的问题,并将其强制转换为关闭(power off)状态。 挂起状态,也称为暂停状态,是 VMware 提供的一种功能,使用户能够暂时停止虚拟机...

    C# WinForm多线程开发复习进程.pdf

    ThreadState 在各种情况下的可能取值包括:Aborted、AbortRequested、Background、Running、Stopped、StopRequested、Suspended、SuspendRequested、Unstarted、WaitSleepJoin 等。 在 WinForm 中使用线程时,需要...

Global site tag (gtag.js) - Google Analytics