스레드에 문제가 있습니다. 스레드를 만들고 로그를 쓰고 싶습니다. (이미 구현 된 메서드 쓰기) 이 테스트는 단위 테스트이므로 실행하면 멋지게 작동합니다. 그러나 예외가 나타납니다. System.AppDomainUnloadedException : 언로드 된 AppDomain에 액세스하려고했습니다. 테스트가 스레드를 시작했지만 중지하지 않은 경우에 발생할 수 있습니다. 완료되기 전에 테스트로 시작된 모든 스레드가 중지되었는지 확인하십시오.Thread.Suspend()가 사용되지 않음
그래서 ThreadC.Suspend()를 사용하려고 시도했지만 오류가 사라졌지 만 Suspend는 사용하지 않습니다 .. 어떻게 해결할 수 있습니까?
public void TestMethod1()
{
try
{
LogTest logTest = new LogTest(new FileLog());
logTest.PerformanceTest();
logTest = new LogTest(new CLogApi());
logTest.PerformanceTest();
logTest = new LogTest(new EmptyLog());
logTest.PerformanceTest();
}
catch (Exception)
{
Assert.IsTrue(false);
}
}
public class LogTest
{
private readonly Log log;
private int numberOfIterations = 5;
public LogTest(Log log)
{
this.log = log;
}
public void PerformanceTest()
{
for (int i = 0; i < this.numberOfIterations; i++)
{
try
{
Thread threadC = Thread.CurrentThread;
threadC = new Thread(this.ThreadProc);
threadC.Name = i.ToString();
threadC.Start();
// threadC.IsBackground = true;
}
catch (Exception)
{
Assert.IsTrue(false);
}
}
}
private void ThreadProc()
{
try
{
this.log.Write(" Thread : " + Thread.CurrentThread.Name.ToString());
this.log.Write(" Thread : " + Thread.CurrentThread.Name.ToString());
this.log.Write(" Thread : " + Thread.CurrentThread.Name.ToString());
this.log.Write(" Thread : " + Thread.CurrentThread.Name.ToString());
}
catch (Exception)
{
Assert.IsTrue(false);
}
}
}
당신은 할 수 없습니다 Task.WaitAll 모든 작업 기다릴 수 스레드를 사용하여. 슬레지 해머 솔루션을 사용하지 말고 AutoResetEvent를 사용하십시오. ThreadProc()의 끝에서 Set() 메서드를 호출하고 Start() 호출 후에 WaitOne() 메서드를 호출합니다. –