简体   繁体   English

C#1.1:监视工作线程

[英]C# 1.1: Monitoring worker threads

Using the .Net Framework 1.1, what options are available for monitoring threads from other threads? 使用.Net Framework 1.1,哪些选项可用于监视其他线程中的线程? I am attempting to deal with a shortcoming of the threading implementation in 1.1 wherein unhandled exceptions will cause threads to die silently. 我正在尝试解决1.1中的线程实现的缺点,其中未处理的异常将导致线程无声地死亡。 In 2.0 and later this had been corrected so that any unhandled exception on any thread will cause the entire application to die, I beleive. 我相信,在2.0及更高版本中,此问题已得到纠正,因此任何线程上任何未处理的异常都将导致整个应用程序死亡。

When threads in my application die, I would like to retrieve as much context as possible from the main thread: the method they were executing, a stack trace, etc. I can determine when they die but retrieving context has proven difficult. 当我的应用程序中的线程死亡时,我想从主线程中检索尽可能多的上下文:它们正在执行的方法,堆栈跟踪等。我可以确定它们何时死亡,但事实证明检索上下文很困难。 I have tried registering a handler for the AppDomain.CurrentDomain.UnhandledException event. 我尝试注册AppDomain.CurrentDomain.UnhandledException事件的处理程序。 But I am not getting called back when events occur. 但是当事件发生时,我没有得到回电。 This is probably due to a limitation with the API I'm developing for. 这可能是由于我正在开发的API的限制。

Besides this event, what options are available to get context from threads as they die on another thread? 除此事件外,当线程在另一个线程上死亡时,可以使用哪些选项从线程获取上下文?

How are you creating these threads? 您如何创建这些线程? Are you adding a delegate to the thread pool? 您是否正在向线程池添加委托? IF so, you could create a wrapper method that takes the delegate provided and wraps another delegate around it which takes care of your try / catch, and then adds that new delegate to the thread pool queue. 如果是这样,则可以创建一个包装器方法,该方法将提供的委托作为对象,并在其周围包装另一个委托,以处理您的try / catch,然后将该新委托添加到线程池队列中。 That way you could put in your error handling code in that second delegate. 这样,您可以将错误处理代码放入第二个委托中。

You could try add a thread exception handler: 您可以尝试添加线程异常处理程序:

The System.Threading.ThreadExceptionEventArgs e will contain the information about the unhandled exception. System.Threading.ThreadExceptionEventArgs e将包含有关未处理的异常的信息。

// Setup Default Thread Exception Handler
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);


static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    // Insert Code
}

Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx 来源: http//msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx

How about an incrementing count for each thread in a map indexed by the thread ID? 由线程ID索引的映射中的每个线程的递增计数如何? When it stops incrementing, it's dead and time to kill and/or restart it. 当它停止增加时,就死了,该死的时间和/或重新启动它。 The map can contain everything you need to manage all of that as well and sharing info between the hosting thread and those nasty little children. 该地图可以包含管理所有内容以及在托管线程和那些讨厌的小孩之间共享信息所需的一切。

I would create a method which will take the callback you would normally pass to a thread and then have that called inside some wrapper code which you would use to track exceptions in the thread, something like this: 我将创建一个方法,该方法将接受通常传递给线程的回调,然后在一些包装器代码中调用该方法,以用于跟踪线程中的异常,如下所示:

public class ThreadStarter
{
  ThreadStart start;

  public ThreadStarter(ThreadStart start)
  {
    this.start = start
  }

  public void Run()
  {
    // Create the thread.
    Thread t = new Thread(new ThreadStart(InternalRun));
    t.Start();
  }

  private void InternalRun()
  {
    // Wrap in a try/catch.
    try
    {
      // Run the code.
      start();
    }
    catch (Exception e)
    {
      // Process exception here.
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM