我认为我正在处理的未处理的 FileLoadException

发布于 2024-09-28 22:18:46 字数 350 浏览 2 评论 0原文

我有一个线程,我像这样初始化:

Utility.Log("1");

myThread = new Thread(new ThreadStart(delegate
{
    Utility.Log("2");

然后是线程的其余部分的执行。奇怪的是,尽管整个事情都包含在 try/catch 中,但我在日志文件中只看到 1(没有 2),并且收到未处理的 System.IO.FileLoadException。我也尝试过将委托的整个主体包装在 try/catch 中,但我仍然收到该异常,并且事件查看器表示该异常的最顶层方法是该方法。这很奇怪。

我有什么想法可以追踪这个问题,或者至少正确地捕获异常吗?

I have a thread that get I initialize like this:

Utility.Log("1");

myThread = new Thread(new ThreadStart(delegate
{
    Utility.Log("2");

and then the rest of the thread's execution. The weird thing is, despite that whole thing being wrapped in a try/catch, I'm only seeing a 1 in my log file (no 2), and I'm getting an unhandled System.IO.FileLoadException. I've tried also wrapping the entire body of the delegate in a try/catch, but I'm still getting that exception, and the Event Viewer is saying that the top most method of the exception is that method. It's very bizarre.

Any ideas of how I can track this down, or at least to properly catch the exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倒带 2024-10-05 22:18:46

FileLoadException 是一个非常严重的事故。当 JIT 编译器尝试编译您在线程中运行的代码时,会引发该错误。 try/catch 对无法捕获此异常,因为它是在代码开始执行之前引发的。换句话说,它会在您进入 try 块之前进行轰炸。鉴于这是一个线程,您无法阻止程序崩溃到桌面。您最后的喘息是 AppDomain.UnhandledException,e.ExceptionObject 的 InnerException 属性告诉您真正出了什么问题。

否则这个异常应该总是很容易修复的。这是一个配置问题,JIT 编译器发现一个程序集的版本号错误或者是旧版本的程序集,诸如此类。如果您无法从 AppDomain.UnhandledException 进行诊断,那么 Fuslogvw.exe 工具可以向您展示它如何找到错误的程序集。完全重建您的解决方案应该是解决问题的一半。

A FileLoadException is a pretty serious mishap. It is raised when the JIT compiler tries to compile the code that you run in your thread. A try/catch pair cannot catch this exception because it is raised before the code starts executing. In other words, it bombs before you enter the try block. Given that this is a thread, you can't stop your program from crashing to the desktop. The last gasp you have is AppDomain.UnhandledException, the e.ExceptionObject's InnerException property tells you what is really going wrong.

This exception should otherwise always be easily fixable. It is a configuration issue, the JIT compiler finds an assembly that has the wrong version number or is an old version of the assembly, something like that. If you can't diagnose it from AppDomain.UnhandledException then the Fuslogvw.exe tool can show you how it is finding the wrong assembly. A full rebuild of your solution ought to be halfway to a fix.

﹏雨一样淡蓝的深情 2024-10-05 22:18:46

您只发布了部分代码,因此不可能回答您的问题。这是一般性建议。

切勿对线程使用匿名方法。很容易做错事,线程中未捕获的异常可能会导致整个应用程序崩溃。

public void MyMethod()
{
    _myThread = new Thread(WorkerThread);
    _myThread.Start();
}

public void WorkerThread(object state)
{
    try
    {
      Utility.Log("2");
    }
    catch (Exception e)
    {
       //log error
    }
}

You did only post a part of your code, therefore it's quite impossible to answer your question. So here's a general advice.

Never use anonymous methods for threads. It's very easy to do something wrong and uncaught exceptions in threads can bring your whole application down.

public void MyMethod()
{
    _myThread = new Thread(WorkerThread);
    _myThread.Start();
}

public void WorkerThread(object state)
{
    try
    {
      Utility.Log("2");
    }
    catch (Exception e)
    {
       //log error
    }
}
以酷 2024-10-05 22:18:46

原始的 try..catch 绝对不会捕获新线程中的异常,而只会捕获原始线程中的异常。

如果您想弄清楚子线程中发生了什么,则必须为其提供自己的异常处理。听起来您尝试按照“我也尝试将委托的整个主体包装在 try/catch”中来执行此操作,但是需要更改代码来确认其正确性。

您还应该能够通过调试子线程来缩小范围。

The original try..catch will not definitely catch exceptions in your new thread, only in the originating thread.

If you want to work out what's going on in the child thread, you'll have to give it its own exception handling. It sounds like you tried to do this per "I've tried also wrapping the entire body of the delegate in a try/catch", but that altered code would be needed to confirm its correctness.

You should also be able to narrow this down by debugging into the child thread.

岁吢 2024-10-05 22:18:46

您需要添加一个线程异常事件处理程序:

只需在 application.run 之前添加该处理程序,您就可以捕获所有未处理的线程异常。

来源:msdn:

[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Main(string[] args)
{
    // Add the event handler for handling UI thread exceptions to the event.
    Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);

    // Set the unhandled exception mode to force all Windows Forms errors to go through
    // our handler.
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    // Add the event handler for handling non-UI thread exceptions to the event. 
    AppDomain.CurrentDomain.UnhandledException +=
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    // Runs the application.
    Application.Run(new ErrorHandlerForm());
}

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

You need add a thread exception event handler:

Just add the handler before application.run and you can catch all unhandled thread exceptions.

Source from msdn:

[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Main(string[] args)
{
    // Add the event handler for handling UI thread exceptions to the event.
    Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);

    // Set the unhandled exception mode to force all Windows Forms errors to go through
    // our handler.
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    // Add the event handler for handling non-UI thread exceptions to the event. 
    AppDomain.CurrentDomain.UnhandledException +=
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    // Runs the application.
    Application.Run(new ErrorHandlerForm());
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文