目前我正在尝试使用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-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).
如果我在Visual Studio中自行运行UpdateDirectoryExceptionTest,它会毫无问题地通过,但是当我运行整套单元测试时,我收到上述错误。这是我最近开始测试的功能,我认为这与错误有关:
public Thread StartClassThread(ref 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.ThreadState == System.Threading.ThreadState.Unstarted)
{
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;
}
以下是我从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>
有没有人对如何解决这些错误有任何想法? TIA。
UPDATE :以下是clsThread.cs的完整源代码(请注意第23行是objThreadMethod();):
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
}
}
答案 0 :(得分:3)
问题似乎是{23}在第23行为空。语法objThreadMethod
实际上是objThreadMethod()
的简写,因此如果objThreadMethod.Invoke()
,您将获得NullReferenceException
执行该行时objThreadMethod
为空。
至于为什么它为null,我猜你已经用非空StartClassThread
和空ThreadObject
调用MethodObject
方法,这导致了以下块将objMethod.objThreadMethod
设置为空MethodObject
后执行:
ThreadObject = null;
ThreadObject = new Thread( new System.Threading.ThreadStart( objMethod.Execute ) );
ThreadObject.Start();
答案 1 :(得分:1)
我认为对于初学者来说,您希望将测试配置为与多线程公寓一起运行:
http://msdn.microsoft.com/en-us/library/ms404663.aspx
我会尝试一下,看看你是否继续有例外。