Click here to Skip to main content
15,918,003 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to use resume()to execute remaining iterations?

C#
namespace CAthreads
{

    class clsthread2
    {
        public void thread1()
        {
            for (int i = 0; i < 10; i++)
            {

                Thread t1 = Thread.CurrentThread;
             Console.WriteLine(t1.Name + "=" + i);
                if (i == 5)
                {
                 Console.WriteLine(t1.Name + " is going to suspend");
                 t1.Suspend();
                  
                }
            }
        }

               class Class1
        {
            static void Main()
            {
                clsthread2 obj1 = new clsthread2();
                ThreadStart tstart1 = new ThreadStart(obj1.thread1);
                ThreadStart tstart2 = new ThreadStart(obj1.thread1);
                Thread thr1 = new Thread(tstart1);
                Thread thr2 = new Thread(tstart1);
                thr1.Name = "NTR";
                thr2.Name = "ANR";
                thr1.Start();
                thr2.Start();
              Console.Read();
            }
        }
    }
Posted
Updated 30-May-14 22:10pm
v2

For starters, Suspend and Resume are obsolete, and according to MSDN: "will be removed in a future release." (http://msdn.microsoft.com/en-us/library/tttdef8x(v=vs.110).aspx[^])

This means you should not be using them, as they may not work tomorrow!
You should be using WaitHandles[^] instead - they are much more flexible and not obsolete...

Try WaitOne[^] - it includes an example.
 
Share this answer
 
If you wanted to Resume thread into other class then this will help you...

I have updated your code....

C#
namespace CAthreads
{

    class clsthread2
    {
        Thread t1;
        public void thread1()
        {
            for (int i = 0; i < 10; i++)
            {

                t1 = Thread.CurrentThread;
                Console.WriteLine(t1.Name + "=" + i);
                if (i == 5)
                {
                    Console.WriteLine(t1.Name + " is going to suspend");
                    t1.Suspend();

                }
            }
        }

       // Created new method to resume thread...
        public void ResumeThread()
        {
            if (t1.ThreadState == ThreadState.Suspended)
            {
                t1.Resume();
            }
        }
    }

    class Class1
    {
        static void Main()
        {
            clsthread2 obj1 = new clsthread2();
            obj1.ResumeThread();   //Method of Class clsthread2
            
            ThreadStart tstart1 = new ThreadStart(obj1.thread1);
            ThreadStart tstart2 = new ThreadStart(obj1.thread1);
            Thread thr1 = new Thread(tstart1);
            Thread thr2 = new Thread(tstart1);
            thr1.Name = "NTR";
            thr2.Name = "ANR";
            thr1.Start();
            thr2.Start();
            Console.Read();
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900