countrymeister
asked on
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
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());
}
}
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
Why don't you just wait a while before starting the second thread?
threadMain.Start();
Thread.Sleep(1000);
threadSecondary.Start();
threadMain.Start();
Thread.Sleep(1000);
threadSecondary.Start();
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?