Click here to Skip to main content
15,913,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
iam working with thread and i have created thread by background process.

myRequirement: Iam looping the value. Each loop i want to make thread sleep for 5000sec. But it doesnt call thread method?.

Iam getting output immediately.

Kindly solve this issue. my code follows.
C#
private void button_PrintNumber_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 10; i++)
            {
                listBox1.Items.Add(i);
                Thread backGroundThread = new Thread(DoTime);
                backGroundThread.Start();
            }
        }
        private void DoTime() 
        {
             Thread.Sleep(5000);
        }


What I have tried:

C#
private void button_PrintNumber_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 10; i++)
            {
                listBox1.Items.Add(i);
                Thread backGroundThread = new Thread(DoTime);
                backGroundThread.Start();
            }
        }
        private void DoTime() 
        {
             Thread.Sleep(5000);
        }
Posted
Updated 26-Aug-16 2:41am

1 solution

Well yes. You will.
That's the whole idea of using a separate Thread - it lets the main thread continue with what it was doing while the new one does something different.
So your code kicks off ten threads, all of which suspend themselves for five seconds without affecting the main thread which continues as if the other threads didn't exist.
If you want the main thread to pause, you need to use Sleep on main thread code - but I really don't recommend that!
 
Share this answer
 
Comments
vinodh muthusamy 29-Aug-16 1:17am    
Did you understand my question which I have asked.

I want to pause for 5000 sec for each loop. But it does not do that..

What is the solution for this
OriginalGriff 29-Aug-16 4:16am    
Read what I said...

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