Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I'm new to Windows Service that I'm trying to create in Visual Studio 2012. I want two methods to keep running in background in parallel/disconnected from each other. How can I achieve this purpose. A little demonstration will be highly appreciated. My target OS is Windows Server 2008 R2. Please help!


Thanks a lot in Advance!

Best Regards,
Sunny
Posted

1 solution

In .net4 you can do the following:
C#
public void main()
{
   ... 
   bool run = true; // to control the exit from the loops

   Task.Factory.StartNew(()=>{
       while(run)
       {
          ... // your code here
          Thread.Sleep(10);// sleep for 10ms
       }
   });
}
 
Share this answer
 
Comments
Sunny_Kumar_ 24-May-13 7:19am    
Thanks for your valuable time & help Mr. Gholam. Can you please let me know how can we monitor these threads so that if dies anyhow, a new one will spin off??
Mehdi Gholam 24-May-13 7:23am    
The code will run forever, you can put a try..catch in the while and all errors will be handled.
Sunny_Kumar_ 24-May-13 7:56am    
Thank you so much for the help :)

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