Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 我想我正在处理的未处理FileLoadException_.net_Multithreading_Fileloadexception - Fatal编程技术网

.net 我想我正在处理的未处理FileLoadException

.net 我想我正在处理的未处理FileLoadException,.net,multithreading,fileloadexception,.net,Multithreading,Fileloadexception,我有一个线程,我初始化如下: Utility.Log("1"); myThread = new Thread(new ThreadStart(delegate { Utility.Log("2"); 然后执行线程的其余部分。奇怪的是,尽管整个过程都被包装在一个try/catch中,但我在2号日志文件中只看到了1,并且我得到了一个未处理的System.IO.FileLoadException。我也尝试过用try/catch包装委托的整个主体,但是我仍然得到了那个异常,事件查看器说异常的

我有一个线程,我初始化如下:

Utility.Log("1");

myThread = new Thread(new ThreadStart(delegate
{
    Utility.Log("2");
然后执行线程的其余部分。奇怪的是,尽管整个过程都被包装在一个try/catch中,但我在2号日志文件中只看到了1,并且我得到了一个未处理的System.IO.FileLoadException。我也尝试过用try/catch包装委托的整个主体,但是我仍然得到了那个异常,事件查看器说异常的最顶层方法就是那个方法。这很奇怪


你知道我该如何追踪这一点,或者至少正确捕获异常吗?

你只发布了部分代码,因此不可能回答你的问题。所以这里有一个一般性的建议

不要对线程使用匿名方法。很容易出错,线程中未捕获的异常会导致整个应用程序宕机

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

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

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

不要对线程使用匿名方法。很容易出错,线程中未捕获的异常会导致整个应用程序宕机

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

public void WorkerThread(object state)
{
    try
    {
      Utility.Log("2");
    }
    catch (Exception e)
    {
       //log error
    }
}
最初的try..catch肯定不会捕获新线程中的异常,只会捕获原始线程中的异常

如果您想了解子线程中发生了什么,就必须给它自己的异常处理。听起来像是你试图按照我的想法来做的,我也尝试过用try/catch来包装整个委托体,但是需要修改代码来确认它的正确性

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

原始try..catch肯定不会捕获新线程中的异常,只会捕获原始线程中的异常

如果您想了解子线程中发生了什么,就必须给它自己的异常处理。听起来像是你试图按照我的想法来做的,我也尝试过用try/catch来包装整个委托体,但是需要修改代码来确认它的正确性


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

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

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

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

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

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


否则,此异常应始终易于修复。这是一个配置问题,JIT编译器发现程序集的版本号错误或是程序集的旧版本,诸如此类。如果无法从AppDomain.UnhandledException对其进行诊断,则Fuslogvw.exe工具可以向您显示如何查找错误的程序集。解决方案的全面重建应该是修复的一半

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


否则,此异常应始终易于修复。这是一个配置问题,JIT编译器发现程序集的版本号错误或是程序集的旧版本,诸如此类。如果无法从AppDomain.UnhandledException对其进行诊断,则Fuslogvw.exe工具可以向您显示如何查找错误的程序集。解决方案的全面重建应该是修复的一半

FileLoadException收到了什么消息?堆栈跟踪显示了什么?FileLoadException得到了什么消息?堆栈跟踪显示了什么?