Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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
C# Thread.Suspend()已过时_C#_.net_Multithreading_Suspend - Fatal编程技术网

C# Thread.Suspend()已过时

C# Thread.Suspend()已过时,c#,.net,multithreading,suspend,C#,.net,Multithreading,Suspend,我有一个线程问题,我想创建n个线程并写一个日志(方法write,已经实现) 这是一个单元测试,当我运行它时,它工作得很好,但是出现了一个异常: System.AppDomainUnloadexException:尝试访问卸载的AppDomain。如果测试启动了一个线程但没有停止它,则可能发生这种情况。确保测试启动的所有线程在完成之前都已停止 所以,我尝试使用ThreadC.Suspend(),错误消失了,但mehod Suspend已经过时了。。 我怎样才能修好它 public void

我有一个线程问题,我想创建n个线程并写一个日志(方法write,已经实现) 这是一个单元测试,当我运行它时,它工作得很好,但是出现了一个异常: System.AppDomainUnloadexException:尝试访问卸载的AppDomain。如果测试启动了一个线程但没有停止它,则可能发生这种情况。确保测试启动的所有线程在完成之前都已停止

所以,我尝试使用ThreadC.Suspend(),错误消失了,但mehod 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);
            }
        }
    }
public void TestMethod1()
{
尝试
{
LogTest LogTest=newlogtest(newfilelog());
logTest.PerformanceTest();
logTest=newlogtest(newclogapi());
logTest.PerformanceTest();
logTest=新的logTest(new EmptyLog());
logTest.PerformanceTest();
}
捕获(例外)
{
Assert.IsTrue(false);
}
}
公共类日志测试
{
私有只读日志;
私有整数迭代次数=5;
公共日志测试(日志)
{
this.log=log;
}
公共空间绩效测试()
{
for(int i=0;i
1:应该使用“Assert.Fail()”而不是Assert.IsTrue(false)

2:如果使用过时的方法,请阅读Microsoft文档。它们编写您可以使用的内容。“Thread.Suspend已被弃用。请使用System.Threading中的其他类(如监视器、互斥体、事件和信号量)来同步线程或保护资源。”

3:如果我理解正确的话,您希望杀死所有正在运行的线程或等待它们。您可以使用“Thread.Join()” 您可以将所有线程存储在一个数组中,也可以在末尾列出一个连接所有线程的列表


4:您可以使用异步模式,通过Task.WaitAll(Tasks)等待所有任务,而不是使用线程。

您不能这样做。不要使用大锤式解决方案,使用AutoResetEvent。在ThreadProc()的末尾调用它的Set()方法,在Start()调用之后调用它的WaitOne()方法。