Click here to Skip to main content
15,922,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
My project is window service on C#, contains 2 service:
- 1 manager service: can start slave service by some codition.
- Many slave service: will do something.

1 of my slave services has code as follow:

C#
for(i:1-n)
{
	doSomeThing();
	Thread.Sleep(3000); // wait a bit
}


But after installed, the service only run the 1st iteration of for, I tried to unit test the function and it work right.
When I clear the line "Thread.Sleep(3000);" then the service run with all iterations.
So, do "Thread.Sleep()" cause stop of the window service? What is a alternative solution of Thread.sleep for window service?

I tried google for days but I've not found any solution.
Could you help me.
Thank in advance.
TuanNM
Posted
Updated 14-Feb-12 23:37pm
v2
Comments
Herman<T>.Instance 15-Feb-12 4:31am    
is there more code involded?

Try the following:
C#
while(run) // a global boolean which you can set to false to end the loop
{ 
   doSomething();
   Thread.Sleep(3000);
}
 
Share this answer
 
No it won't. Something else is causing the issue here. For example, is your master service killing the slaves after a timeout? Is the service communicating with something else which regards 3 seconds as inactivity and closes the communication channel? Does doSomething do something which is time dependent?
 
Share this answer
 
Comments
TuanNGUYEN 15-Feb-12 9:54am    
Thanks, I've found the reason.

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