C# QTAgent32.exe,捕获到未处理的异常,通过Watson报告:System.NullReferenceException
目前,我正在尝试使用MSTest.exe创建多线程功能的单元测试。当我运行测试时,以下是我得到的错误:C# QTAgent32.exe,捕获到未处理的异常,通过Watson报告:System.NullReferenceException,c#,multithreading,visual-studio,mstest,nullreferenceexception,C#,Multithreading,Visual Studio,Mstest,Nullreferenceexception,目前,我正在尝试使用MSTest.exe创建多线程功能的单元测试。当我运行测试时,以下是我得到的错误: A first chance exception of type 'System.NullReferenceException' occurred in ApplicationManagement.exe The thread 'Agent: adapter run thread for test 'UpdateDirectoryExceptionTest' with id 'a78d3e8e
A first chance exception of type 'System.NullReferenceException' occurred in ApplicationManagement.exe
The thread 'Agent: adapter run thread for test 'UpdateDirectoryExceptionTest' with id 'a78d3e8e-e859-43aa-87aa-cf006f736dee'' (0x1150) has exited with code 0 (0x0).
The thread '<No Name>' (0x1bec) has exited with code 0 (0x0).
E, 6788, 86, 2011/10/20, 14:02:36.771, WKSTVMC0006\QTAgent32.exe, Unhandled Exception Caught, reporting through Watson: System.NullReferenceException: Object reference not set to an instance of an object.
at CommonObjects4.clsThread.StartThreadMethod.Execute() in D:\DevProjects\CommonObjects4\classes\clsThread.cs:line 23
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
The program '[6788] QTAgent32.exe: Managed (v4.0.30319)' has exited with code -2 (0xfffffffe).
The program '[1120] ApplicationManagement.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
以下是我从CruiseControl.NET收到的输出,与在Visual Studio中进行测试不同:
<message>Passed UnitTests.clsThreadTest.StartClassThreadTest2</message>
<message>Process 'QTAgent32' [PID 3932] has finished profiling.</message>
<message>Process 'QTAgent32' [PID 5388] has begun profiling.</message>
<message>Error UnitTests.clsUltraCalendarTest.clsUltraCalendarConstructorTest</message>
我认为,对于初学者来说,您希望将测试配置为使用多线程单元运行:
我会尝试一下,看看是否仍然有异常。问题似乎是
objThreadMethod
在第23行为null。语法objThreadMethod()
实际上是objThreadMethod.Invoke()
的简写,因此,如果执行该行时objThreadMethod
为null,则会出现NullReferenceException
至于它为什么为null,我猜您已经使用非nullThreadObject
和nullMethodObject
调用了startclasthread
方法,这导致在将objMethod.objThreadMethod
设置为nullMethodObject
后执行以下块:
ThreadObject = null;
ThreadObject = new Thread( new System.Threading.ThreadStart( objMethod.Execute ) );
ThreadObject.Start();
不过,感谢您的提示,添加后仍会在同一点崩溃。它运行375个测试中的360个,然后其余测试失败。如果我进行线程单元测试,那么所有测试都通过了。嗯……D:\DevProjects\CommonObjects4\classes\clsThread.cs中第23行是什么?(如果您可以添加完整的StartThreadMethod.Execute()方法主体,或者更好的是,添加完整的StartThreadMethod类定义,可能会有所帮助。)感谢您的建议,Nicole。请参阅上面的clsThread.cs代码。是的,我正在编写的测试之一是显式发送空值,以确保它正确处理它。异常不应该被“catch(exception)”代码捕获,而不是导致整个单元测试套件突然提前终止吗?异常不会在线程之间自动传播。异常发生在catch块的单独线程中。如果要在生成的后台线程中捕获并记录异常,则需要在StartThreadMethod.Execute()方法中添加包装try/catch。否则,后台线程上的异常将导致进程停止。(也就是说,我不一定同意系统地防止后台线程上未经处理的异常导致进程崩溃通常是世界上最好的主意。)好的,这是有道理的。谢谢
namespace CommonObjects4
{
public class clsThread
{
#region '" Sub Class "'
public delegate void ThreadMethod();
private class StartThreadMethod
{
public ThreadMethod objThreadMethod;
public void Execute()
{
objThreadMethod(); // this is line 23
}
}
#endregion
#region '" Enumerator Declaration "'
#endregion
#region '" Variable Declaration "'
#endregion
#region '" Property Declaration "'
#endregion
#region '" Function Declaration "'
public Thread StartClassThread(Thread ThreadObject, ThreadMethod MethodObject)
{
System.Threading.Thread startClassThreadReturn = null;
try
{
if (ThreadObject == null && MethodObject == null)
{
throw new ApplicationException("Cannot have both ThreadObject and MethodObject as null parameters in call to StartClassThread.");
}
StartThreadMethod objMethod = new StartThreadMethod();
objMethod.objThreadMethod = MethodObject;
if (!(ThreadObject == null))
{
if (ThreadObject.ThreadState != System.Threading.ThreadState.Stopped)
{
// Do nothing
}
}
if (!(ThreadObject == null))
{
if (ThreadObject.ThreadState == System.Threading.ThreadState.Stopped | ThreadObject.ThreadState == System.Threading.ThreadState.Aborted )
{
ThreadObject = null;
ThreadObject = new Thread( new System.Threading.ThreadStart( objMethod.Execute ) );
ThreadObject.Start();
}
}
else
{
ThreadObject = new Thread( new System.Threading.ThreadStart( objMethod.Execute ) );
ThreadObject.Start();
}
return ThreadObject;
}
catch (Exception excException)
{
ZEGApp.clsMain.objApplicationAudit.AuditMethodError(excException, System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " - " + excException.StackTrace);
}
finally
{
// Do nothing
}
return startClassThreadReturn;
}
#endregion
}
}
ThreadObject = null;
ThreadObject = new Thread( new System.Threading.ThreadStart( objMethod.Execute ) );
ThreadObject.Start();