user7494002
user7494002

Reputation:

Thread.Suspend() is obsolete

I have a problem with thread, I want to create n thread and write a log (with method write, already implemented) This is an unit test, when I run it, it works nice, but an exception appears : System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain. This can happen if the test(s) started a thread but did not stop it. Make sure that all the threads started by the test(s) are stopped before completion.

So, I tried to use ThreadC.Suspend() and error disappears, but mehod Suspend is obsolete.. How can I fix it?

    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);
            }
        }
    }

Upvotes: 1

Views: 783

Answers (1)

Dominik Viererbe
Dominik Viererbe

Reputation: 397

1: You should use "Assert.Fail()" instead Assert.IsTrue(false);

2: Read the Microsoft documentation if you use an obsolete method. They write what you can use instead."Thread.Suspend has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources."

3: If i understand you correctly you want to kill all running threads or wait for them. You can use "Thread.Join()" https://msdn.microsoft.com/de-de/library/95hbf2ta(v=vs.110).aspx You can store all threads in an Array or list an join all threads at the end.

4: Instead using threads you can use the async pattern and wait for all Tasks with Task.WaitAll(tasks) https://msdn.microsoft.com/en-us/library/dd270695(v=vs.110).aspx

Upvotes: 2

Related Questions