• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

C# Windows Service

I have C# Windows Service written and it works fine. When I start the services it executes the commands and does what it needs to do and then stop.

What I need it to do is the following;

1. Listen to when a new message is available via IBM MQ and then start the services automatically.

How can I accomplish this? Should my service not stop and run constantly v/s stopping and then starting back up. Any suggestions, code examples etc; would be greatly appreciated.

Thanks
0
jeffreyjseaman
Asked:
jeffreyjseaman
  • 3
  • 2
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
What I would normally do is keep the service running.

Based on the frequency of the messages, adn the responsiveness you need, put the thread ot sleep before looping.

Essentially:

while (1)
{
  ProcessQueue();
  Thread.CurrentThread.Sleep(x);  //Seconds, minutes hours?
}
0
 
jeffreyjseamanAuthor Commented:
Hi Thanks,

So if I do the Thread.CurrentThread.Sleep(x); // whatever amount of seconds 500 seconds..
Will the Service automatically start back up after the 500 seconds or whatever the set time was?

Thanks
Jeff
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
If it's in a loop yes.

essentially you keep in "running" the entire time.  After the 500 seconds (note sleep is in MS, so it would be 500,000 you would pass in the value for 500 seconds), it would wake itself up and continue to run.  You hold onto any memory you used, but no CPU cycles are wasted during the  sleep time.

Note you should also handle the stop event.

http://code.msdn.microsoft.com/windowsdesktop/CSWindowsService-9f2f568e
0
 
jeffreyjseamanAuthor Commented:
Thank You. I will try this out. Appreciate your help.
0
 
jeffreyjseamanAuthor Commented:
What I wanted.. Thank You...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now