How to put a secondary thread to sleep

I have a main console application which creates two threads
I need to put one of the secondary threads  to sleep. How to I do that

In the code below the main processing cretes  two threads which point to the static method ThreadProc.
I need to put the thread named secondary to sleep

Code sample will be great
static void Main(string[] args)
        {
 Thread threadMain = new Thread(new ThreadStart(ThreadProc));
 Thread threadSecondary = new Thread(new ThreadStart(ThreadProc));      
}        
          
 
private  static void ThreadProc()
        {
            lock (sw)
            {
                sw.WriteLine("Thread {0} started at {1}", Thread.CurrentThread.Name, DateTime.Now.ToLocalTime());
            }
            //Main Database Processing
                           
            string[] tradingArea = new string[1] { strTradingArea };
            DBHelper dbHelper = new DBHelper();
            dbHelper.DBProcedure(strProcessName, minTimeStamp, Thread.CurrentThread.Name, tradingArea);
           
            lock (sw)
            {
                sw.WriteLine("Thread {0} finished at {1}", Thread.CurrentThread.Name, DateTime.Now.ToLocalTime());
            }
                    }

Open in new window

LVL 1
countrymeisterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Göran AnderssonCommented:
Could you specify what you mean by "put to sleep" in this context?

The code in the method doesn't do any lengthy operations. If it takes any time, it's because the code is waiting for some other process to do the work, i.e. the database server. So, the threads are already more or less sleeping, so what do you want to change?
0
countrymeisterAuthor Commented:
Well I want to check if the thread within the ThreadProc method is the thread named secondary.
if it is then call sleep on this thread.

First is this possible, since the ThreadProc method is in the main class and calling thread.sleep will put the main thread to sleep.

Thread threadMain = new Thread(new ThreadStart(ThreadProc));
threadMain.Name = "MAIN"
 Thread threadSecondary = new Thread(new ThreadStart(ThreadProc));      
threadSecondary.Name = "SECONDARY"

so in the ThreadProc static method I need to put the secondary method to sleep.


0
photowhizCommented:
What you are doing is certainly possible, and calling Sleep on a secondary thread will not put the main thread to sleep regardless of where you happen to declare it. It's running on a separate thread, where you wrote the source code is irrelevant.

Setting the name of a thread is possible, but will probably not do what you expect because the thread has already started before you change the name from the main thread.

Another problem is that your main thread ends before your secondary threads have a chance to do anything, and when the main thread ends your secondary threads are disposed. In your sample code they will never get time to do anything.

You can use QueueUserWorkItem to send data to threads, as shown in my sample code. The sample is pretty pointless, though, what are you trying to accomplish?
static void Main(string[] args)
{
    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), "one");
    ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), "two");
    Thread.Sleep(5000);
}
 
static void ThreadProc( object state )
{
    string name = state.ToString();
    if (name.Equals("two"))
        Thread.Sleep(4000);
    Console.WriteLine(name);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
countrymeisterAuthor Commented:
photowiz

Sorry that I posted just a snippet of my code. I do have threadMain.Join and threadSecondary.Join statments, so the main thread will have to wait till they finish.

Basically I want to start two threads, but I want to delay or sleep the second thread for some time, so that the first thread is given a lead in processing
0
Göran AnderssonCommented:
Why don't you just wait a while before starting the second thread?

threadMain.Start();
Thread.Sleep(1000);
threadSecondary.Start();
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.