none
What is the best way to handle a thread from a different thread? RRS feed

  • Question

  • My requirement is to handle a thread from a different thread. I am able to do with thread.Suspend()thread.Resume() and thread.Abort() but I am getting a warning like these methods has been deprecated. Is there any alternative to these methods, I created a dummy console application which is similar to my application. in actual application there is no loop used. its number of steps to execute and every step will call to a different assembly code.    please help to to fix this. Below is my code.

    using System;
    using System.Threading;
    
    namespace ThreadingTest
    {
        internal class Program
        {
            private static Thread mainThread;
    
            private static void Main(string[] args)
            {
                mainThread = Thread.CurrentThread;
    
                Thread connectServerThread = new Thread(new ThreadStart(ConnectServer));
                connectServerThread.Start();
    
                int i = 0;
                while (true)
                {
                    if (i++ % 5000 == 0)
                    {
                        Console.WriteLine(i);
                    }
                }
            }
    
            private static void ConnectServer()
            {
                for (int i = 0; i < 20; i++)
                {
                    Thread.Sleep(2000);
                    if (i % 2 == 0)
                    {
                        mainThread.Suspend();
                    }
                    else
                    {
                        mainThread.Resume();
                    }
                }
                mainThread.Abort();
            }
        }
    }

    Friday, November 29, 2019 7:56 AM

All replies

  • Well it is hard to say best way since there are so many options for threading that it depends on what threads might do and so on.

    If considering only your example using EventWaitHandle class could be one option:

    internal class Program
    {
        private static Thread mainThread;
        private static EventWaitHandle waitHandle;
    
        private static void Main(string[] args)
        {
            mainThread = Thread.CurrentThread;
            waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
    
            Thread connectServerThread = new Thread(new ThreadStart(ConnectServer));
            connectServerThread.Start();
    
            int i = 0;
            while (true)
            {
                if (i++ % 5000 == 0)
                {
                    Console.WriteLine(i);
                    waitHandle.WaitOne();
                }
            }
        }
    
        private static void ConnectServer()
        {
            for (int i = 0; i < 20; i++)
            {
                Thread.Sleep(2000);
                if (i % 2 == 0)
                {
                    waitHandle.Reset();
                }
                else
                {
                    waitHandle.Set();
                }
            }
            mainThread.Abort();
        }
    }


    • Edited by MasaSam Friday, November 29, 2019 10:12 AM
    Friday, November 29, 2019 10:11 AM