遇到过这个exception么? 这个exception是为什么而产生的呢?

下面的代码段来自MSDN, 很有说明性.

 

简单来说, 就是当进程还想继续执行的时候, 发现自己已经被调用过Abort方法了. 既然自己作为线程已经被中止, 就无法执行罗, 于是exception丢了出来.

 

下面的代码来自MSDN, 说明问题:

The following example demonstrates aborting a thread. The thread that receives the ThreadAbortException uses the ResetAbort method to cancel the abort request and continue executing.

using System;
using System.Threading;
using System.Security.Permissions;
   4:  
class ThreadWork {
void DoWork() {
try {
int i=0; i<100; i++) {
); 
  10:                 Thread.Sleep(100);
  11:             }
  12:         }
catch(ThreadAbortException e) {
);
, e.Message);
  16:             Thread.ResetAbort();
  17:         }
); 
  19:         Thread.Sleep(1000);
);
  21:     }
  22: }
  23:  
class ThreadAbortTest {
void Main() {
new ThreadStart(ThreadWork.DoWork);
new Thread(myThreadDelegate);
  28:         myThread.Start();
  29:         Thread.Sleep(100);
);
  31:         myThread.Abort();
  32:         myThread.Join();
); 
  34:     }
  35: }

相关文章:

  • 2021-07-28
  • 2021-09-15
  • 2021-08-29
  • 2021-09-28
  • 2022-12-23
  • 2021-06-15
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2022-03-07
  • 2022-12-23
  • 2022-01-15
相关资源
相似解决方案