public  delegate void myDel();//declare delegate

        static void Main(string[] args)
        {
            myDel Del;
           Thread myThread;
            program2 myOb = new program2();

            Del = new myDel(myOb.helloMethod);
           
            myThread = new Thread(new ThreadStart(Del));
            
            myThread.Start();

            for (int i=0;i<100000;i++)//main thread
            {
                Console.WriteLine(i);
            }
        }
    }
    class program2
    {
        public void helloMethod()
        {
            for (int i = 0; i < 10000; i++)//run 10000 times..second thread
            {
               Console.WriteLine("This is my second thread");
               //Thread.Sleep(2000);
            }
        }
    }

I am trying some simple multithreading. Rather than just using the commented Thread.Sleep(); How could I just suspend this thread using myThread.Suspend()?

I assume I would have to pass myThread as an argument to helloMethod?

To reference the current Thread, you could use Thread.CurrentThread.Suspend(); . The Suspend() method is obsolete however, what are you trying to achieve?

class Program
    {
       public  delegate void myDel(Thread t);//declare delegate

        static void Main(string[] args)
        {
            myDel Del;
           Thread myThread;
            program2 myOb = new program2();

            Del = new myDel(myOb.helloMethod(myThread));
           
            myThread = new Thread(new ThreadStart(Del));
            
            myThread.Start();

            for (int i=0;i<100000;i++)//main thread
            {
                Console.WriteLine(i);
            }
        }
    }
    class program2
    {
        public void helloMethod(Thread t)
        {
            for (int i = 0; i < 10000; i++)//run 10000 times..second thread
            {
               Console.WriteLine("This is my second thread");

               t.Suspend();   //would like to call t.suspend() here
               
            }
        }
    }

So as I have tried to show above..I would like to pass myThread to helloMethod. I would then like, from helloMethod to call t.Suspend()?

I suppose what I am trying to get at is...how can I pass a Thread object from within one thread to another?

What kind of functionality are you trying to accomplish? Usually threads aren't passed around the way you are describing and suspending a thread is not really supported anymore. Suspending is deprecated and should not be used, but Join() might be what you are looking for. I haven't really used it a whole lot, but basically myThread.Join() will 'suspend' the current thread until myThread is complete. It is always used with reference to the current thread so suspending of the thread must be done within that threads scope (ie not with a thread that is passed as a paramater).

So, if you are trying to block the main thread until the second thread is complete...

static void Main(string[] args)
        {
            myDel Del;
           Thread myThread;
            program2 myOb = new program2();
           
            myThread = new Thread(new ThreadStart(myOb.helloMethod));
            
            myThread.Start();

            myThread.Join(); 

            //This will not execute until myThread is complete
            for (int i=0;i<100000;i++)//main thread
            {
                Console.WriteLine(i);
            }
        }
    }
    class program2
    {
        public void helloMethod()
        {
            for (int i = 0; i < 10000; i++)//run 10000 times..second thread
            {
               Console.WriteLine("This is my second thread");
            }
        }
    }

Or if you want to block the second thread until the main thread is done...

class Program
    {
        static void Main(string[] args)
        {
           Thread myThread;
            program2 myOb = new program2();
           
            myThread = new Thread(new ParameterizedThreadStart(myOb.helloMethod));
            
            myThread.Start(Thread.CurrentThread);

            for (int i=0;i<100000;i++)//main thread
            {
                Console.WriteLine(i);
            }
        }
    }
    class program2
    {
        public void helloMethod(Thread t)
        {
            t.Join(); 
            //This will not execute until T is complete - but since T is the main thread this will technically never execute, since the program will be closed.
            for (int i = 0; i < 10000; i++)//run 10000 times..second thread
            {
               Console.WriteLine("This is my second thread");               
            }
        }
    }
commented: Thanks a lot. You have cleared this up for me! +4

I still cannot compile this code?

I get "no overload for 'helloMethod' matches delegate 'System.Threading.ParameterizedThreadStart'

I still cannot compile this code?

I get "no overload for 'helloMethod' matches delegate 'System.Threading.ParameterizedThreadStart'

Sorry, ParamaterizedThreadStart requires that the function called take data of type 'object'. I assume this was done to keep things generic...

void helloMethod(Thread T)

should instead be:

void helloMethod(object T)
{
   Thread myThread = (Thread)t;
}

I make mistakes like that all the time on here since I am usually too busy/lazy to try to compile the code before I post it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.