Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to find out the best alternative to windows service application. i have some task to be executed continuously based on time interval.

I don't want to go with windows installer or not to create separate thread to achieve it.

Please suggest some best approach to achieve this...

my actual requirement is i am using RabbitMQ for message transmission. it has send and receive queues. Receive queue continuously to be monitored for checking the availability of messages in the queue. for this i need to write some thing like windows service component.

Please suggest some best approach so that i can monitor Receive Message Queue continuously.

Thanks
Posted

1 solution

so: No Service & no extra threads?

Polling:

C#
DateTime _time = DateTime.Now;
...


private void MessagePoll(int intervalSeconds){

 Timespan timespan = Timespan.FromSeconds(intervalSeconds)

 //infinite thread.  You have no way to stop this without a second thread
 while(true){
  //If the time interval has not been reached - wait
  while(DateTime.Now-_time < timespan ){
   Thread.Sleep(1000);
  }
  //Do the work
  PollMessages();
  reset the time.
  _time = DateTime.Now;
 }

}



You can also just use Thread.Sleep(intervalSeconds*1000)
 
Share this answer
 
v2
Comments
KVPalem 21-May-15 4:59am    
Thanks for your response.
Can we use above code in as part of WCF Service or Web Application? Do we need to call MessagePoll method in Application_Start? I mean after deploying service or application in IIS web site does above code still valid? Dont we require any separate installer?

Thanks
Andy Lanng 21-May-15 5:02am    
If you run this in the same thread as the web application then the application could freeze. You need a threaded system.

I suggest you use a BackgroundWorker. It's a simple single background thread that will run this code without causing your application to freeze.

If you use the BackgroundWorker then starting it in the Application_Start is fine

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