I want to be able to check if a certain service is running (say it had a Display name - ServiceA). I want my program to check say every 5 mins that the service is still running. If it is fine, it will loop and wait another 5 mins and then check again. If it finds that ServiceA has stopped I want the program to email me and say...ServiceA has stopped running. Below I have the code which I have done so far which is able to pull all the current services running and there actual display name back to the console. Anyone any ideas on the code/logic needed for what I need above?
namespace ServicesChecker
{
class Program
{
static void Main(string[] args)
{
ServiceController[] scServices;
scServices = ServiceController.GetServices();
Console.WriteLine("Services running on the local computer:");
foreach (ServiceController scTemp in scServices)
{
if (scTemp.Status == ServiceControllerStatus.Running)
{
Console.WriteLine();
Console.WriteLine(" Service : {0}", scTemp.ServiceName);
Console.WriteLine(" Display name: {0}", scTemp.DisplayName);
}
}
//Create a Pause....
Console.ReadLine();
}
}
}
Put every service's name in an array and check if your wanted name is running
List<string> arr = new List<string>();
foreach (ServiceController scTemp in scServices)
{
if (scTemp.Status == ServiceControllerStatus.Running)
{
arr.add(scTemp.ServiceName);
}
}
if (arr.Contains("YourWantedName")
{
// loop again
}
else
{
// send mail
}
There's no need to iterate over all services, if you know which one you're looking for: you can instantiate ServiceController with the service name.
As for sending an email: take a look at the System.Net.Mail.MailMessage class.
NB: You know that you can also just configure the service to trigger an action if it fails?
You will need to track the state of the service which will require some sort of storage. The simplest is probably an XML file that tracks the status of the service, maybe a schema like this
<services>
<service name="service1" last-check="12/21/2011 13:00:05" last-status="running" />
...
</services>
Your monitoring app, will wake up find the status of the services it is interested in, and check to see what that service's previous status was. If the status was running, but is currently stopped, send the email. If the service wasn't found add it to the list of services.
Persisting the status of the services to disk protects you in the case when your monitoring app goes down.
Heres an example of a service which does quite something similar. Should be simple to adapt it to your needs..
public partial class CrowdCodeService : ServiceBase
{
private Timer stateTimer;
private TimerCallback timerDelegate;
AutoResetEvent autoEvent = new AutoResetEvent(false);
public CrowdCodeService()
{
InitializeComponent();
}
int secondsDefault = 30;
int secondsIncrementError = 30;
int secondesMaximum = 600;
int seconds;
protected override void OnStart(string[] args)
{
Loggy.Add("Starting CrowdCodeService.");
timerDelegate = new TimerCallback(DoSomething);
seconds = secondsDefault;
stateTimer = new Timer(timerDelegate, autoEvent, 0, seconds * 1000);
}
static bool isRunning = false;
// The state object is necessary for a TimerCallback.
public void DoSomething(object stateObject)
{
if (CrowdCodeService.isRunning)
{
return;
}
CrowdCodeService.isRunning = true;
AutoResetEvent autoEvent = (AutoResetEvent)stateObject;
try
{
////// Do your work here
string cs = "Application";
EventLog elog = new EventLog();
if (!EventLog.SourceExists(cs))
{
EventLog.CreateEventSource(cs, cs);
}
elog.Source = cs;
elog.EnableRaisingEvents = true;
elog.WriteEntry("CrowdCodes Service Error:" + cmd.Message.ToString(), EventLogEntryType.Error, 991);
}
}
finally
{
CrowdCodeService.isRunning = false;
}
}
protected override void OnStop()
{
Loggy.Add("Stopped CrowdCodeService.");
stateTimer.Dispose();
}
}
Related
I've recently started with .Net remoting and I have managed to get working with some simple tutorials such as building a library dll that works as a calculator which the client can access and use(https://www.youtube.com/watch?v=Ve4AQnZ-_H0).
What I'm looking for to understand is how I could access current information that is held on the server. For example if I have this simple part running on the server:
int x = 0;
while (!Console.KeyAvailable)
{
x++;
System.Threading.Thread.Sleep(1000);
Console.WriteLine(x);
}
What I found so far is that the dll built is only returning a static result, such as with the calculator. I'd want to be able to tell how much x is on the server at any given time, through the client.
I don't know if I'm being clear enough but I'll try to explain better if it's needed.
In the following Server implementation demonstrates how you can keep state between calls.
// this gets instantiated by clients over remoting
public class Server:MarshalByRefObject
{
// server wide state
public static int Value;
// state only for this instance (that can be shared with several clients
// depending on its activation model)
private StringBuilder buildup;
// an instance
public Server()
{
buildup = new StringBuilder();
Console.WriteLine("server started");
}
// do something useful
public int DoWork(char ch)
{
Console.WriteLine("server received {0}", ch);
buildup.Append(ch);
return Value;
}
// return all typed chars
public string GetMessage()
{
Console.WriteLine("server GetMessage called") ;
return buildup.ToString();
}
// how long should this instance live
public override object InitializeLifetimeService()
{
// run forever
return null;
}
}
Notice the override InitializeLifetimeService. If you don't control this, your instance will get torn down after 5 minutes.
To use the above class we use the following code to get a listener up and running, including some of your logic. Don't forget to add an reference to the assembly System.Runtime.Remoting.
static void Main(string[] args)
{
// which port
var chn = new HttpChannel(1234);
ChannelServices.RegisterChannel(chn, false);
// Create only ONE Server instance
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Server), "server", WellKnownObjectMode.Singleton);
Server.Value = 0;
while (!Console.KeyAvailable)
{
Server.Value++;
System.Threading.Thread.Sleep(1000);
Console.WriteLine(Server.Value);
}
}
When this code runs, it should listen on your local box on port 1234 for connections. On first run I had to disable the firewall, allow that port to pass the local firewall.
A client implementation that uses the Server might look like this. Don't forget to add an reference to the assembly System.Runtime.Remoting.
static void Main(string[] args)
{
var chn = new HttpChannel();
ChannelServices.RegisterChannel(chn, false);
RemotingConfiguration.RegisterWellKnownClientType(
typeof(Server),
"http://localhost:1234/server");
Console.WriteLine("Creating server...");
var s = new Server();
Console.WriteLine("type chars, press p to print, press x to stop");
var ch = Console.ReadKey();
while(ch.KeyChar != 'x')
{
switch(ch.KeyChar )
{
case 'p':
Console.WriteLine("msg: {0}", s.GetMessage());
break;
default:
Console.WriteLine("Got value {0} ", s.DoWork(ch.KeyChar));
break;
}
ch = Console.ReadKey();
}
Console.WriteLine("stopped");
}
If you compile and run this your result can look like this:
I developed a http server via console application in C# and decided to turn it into a Windows service to be able to initialize it without the need to login the machine.
I followed all the steps in How to create Windows Service and chose the account as "Local System", but when I install in my server machine and push the start button it takes a while and gives the following error:
Erro 1053: The service did not respond to the start or control request in timely fashion.
After that, the service status stays stuck in "starting" and the application don't work and I can't even stop the service anymore.
Trying to work around this problem, I changed it to "Network Service", so it started normally, but the application was not listening in the port I set when I checked in the prompt with the command "netstat -an". But the application listens normally if i run it as a console application.
So I am looking for an answer to one of these two questions:
What should I do to make the service starts properly with a Local System account?
If I decide to use Network service account, what should I care about to guarantee that my service works properly as a server?
When I converted my console application to windows service I simply put my code directly in the OnStart method. However, I realized the OnStart method should start the service, but needs to end some time to the service indeed start. So I created a thread that runs my service and let the OnStart method finish. I tested and the service worked just fine. Here is how it was the code:
protected override void OnStart(string[] args)
{
Listener(); // this method never returns
}
Here is how it worked:
protected override void OnStart(string[] args)
{
Thread t = new Thread(new ThreadStart(Listener));
t.Start();
}
But I still don't understand why the service ran (passed the "starting" status, but didn't work) when I used network service account. If anyone knows, I'll be glad to know the reason.
If you have a service that is not responding or showing pending in Windows services that you are unable to stop, use the following directions to force the service to stop.
Start -> Run or Start -> type services.msc and press Enter
Look for the service and check the Properties and identify its service name
Once found, open a command prompt. Type sc queryex [servicename]
Identify the PID (process ID)
In the same command prompt type taskkill /pid [pid number] /f
Find PID of Service
sc queryex <SERVICE_NAME>
Give result's below
SERVICE_NAME: Foo.Services.Bar TYPE : 10 WIN32_OWN_PROCESS STATE : 2 0 START_PENDING (NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN) WIN32_EXIT_CODE : 0 (0x0) SERVICE_EXIT_CODE : 0 (0x0) CHECKPOINT : 0x0 WAIT_HINT : 0x0 PID : 3976 FLAGS :
Now Kill the Service:
taskkill /f /pid 3976
SUCESS: The process with PID 3976 has been terminated.
Check the Windows Application event log, it could contain some entries from your service's auto generated event source (which should have the same name of the service).
For me it was a while loop that looked at an external queue. The while-loop continued running until the queue was empty. Solved it by calling a timer event directly only when Environment.UserInteractive. Therefore the service could be debugged easily but when running as a service it would wait for the timers ElapsedEventHandler event.
Service:
partial class IntegrationService : ServiceBase
{
private static Logger logger = LogManager.GetCurrentClassLogger();
private System.Timers.Timer timer;
public IntegrationService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
// Add code here to start your service.
logger.Info($"Starting IntegrationService");
var updateIntervalString = ConfigurationManager.AppSettings["UpdateInterval"];
var updateInterval = 60000;
Int32.TryParse(updateIntervalString, out updateInterval);
var projectHost = ConfigurationManager.AppSettings["ProjectIntegrationServiceHost"];
var projectIntegrationApiService = new ProjectIntegrationApiService(new Uri(projectHost));
var projectDbContext = new ProjectDbContext();
var projectIntegrationService = new ProjectIntegrationService(projectIntegrationApiService, projectDbContext);
timer = new System.Timers.Timer();
timer.AutoReset = true;
var integrationProcessor = new IntegrationProcessor(updateInterval, projectIntegrationService, timer);
timer.Start();
}
catch (Exception e)
{
logger.Fatal(e);
}
}
protected override void OnStop()
{
try
{
// Add code here to perform any tear-down necessary to stop your service.
timer.Enabled = false;
timer.Dispose();
timer = null;
}
catch (Exception e)
{
logger.Fatal(e);
}
}
}
Processor:
public class IntegrationProcessor
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
private static volatile bool _workerIsRunning;
private int _updateInterval;
private ProjectIntegrationService _projectIntegrationService;
public IntegrationProcessor(int updateInterval, ProjectIntegrationService projectIntegrationService, Timer timer)
{
_updateInterval = updateInterval;
_projectIntegrationService = projectIntegrationService;
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer.Interval = _updateInterval;
//Don't wait for first elapsed time - Should not be used when running as a service due to that Starting will hang up until the queue is empty
if (Environment.UserInteractive)
{
OnTimedEvent(null, null);
}
_workerIsRunning = false;
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
try
{
if (_workerIsRunning == false)
{
_workerIsRunning = true;
ProjectInformationToGet infoToGet = null;
_logger.Info($"Started looking for information to get");
//Run until queue is empty
while ((infoToGet = _projectIntegrationService.GetInformationToGet()) != null)
{
//Set debugger on logger below to control how many cycles the service should run while debugging.
var watch = System.Diagnostics.Stopwatch.StartNew();
_logger.Info($"Started Stopwatch");
_logger.Info($"Found new information, updating values");
_projectIntegrationService.AddOrUpdateNewInformation(infoToGet);
_logger.Info($"Completed updating values");
watch.Stop();
_logger.Info($"Stopwatch stopped. Elapsed seconds: {watch.ElapsedMilliseconds / 1000}. " +
$"Name queue items: {infoToGet.NameQueueItems.Count} " +
$"Case queue items: {infoToGet.CaseQueueItems.Count} " +
$"Fee calculation queue items: {infoToGet.FeeCalculationQueueItems.Count} " +
$"Updated foreign keys: {infoToGet.ShouldUpdateKeys}");
}
_logger.Info($"Nothing more to get from integration service right now");
_workerIsRunning = false;
}
else
{
_logger.Info($"Worker is already running! Will check back again after {_updateInterval / 1000} seconds");
}
}
catch (DbEntityValidationException exception)
{
var newException = new FormattedDbEntityValidationException(exception);
HandelException(newException);
throw newException;
}
catch (Exception exception)
{
HandelException(exception);
//If an exception occurs when running as a service, the service will restart and run again
if (Environment.UserInteractive)
{
throw;
}
}
}
private void HandelException(Exception exception)
{
_logger.Fatal(exception);
_workerIsRunning = false;
}
}
You can try to increase the windows service timeout with a key in the registry
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control
"ServicesPipeTimeout"=dword:300000 (300 seconds or 5 minutes)
If it doesn't exists it has to be created.
I have windows service that is multi-threded, the threads contains while loop that should always be activated,socket thread which send errors from my other application that installed there, thread which initialize few timers
when i start the thread its start for few sec, then throw exception which says that my service stopped working and ask to send the data to Microsoft.
i don't know what make my service do it, probably the threads but i don't understand what wrong with my implementation
in addition i use custom installer, when the user start the application it will install the service.
tools is a class which contains static mathods.
my code:
public partial class MyService : ServiceBase
{
public static System.Timers.Timer key;
public static System.Timers.Timer browse;
public static System.Timers.Timer sender;
// array of worker threads
Thread[] workerThreads;
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
workerThreads = new Thread[]
{
new System.Threading.Thread(new ThreadStart(InitTimers)),
new System.Threading.Thread(new ThreadStart(Tools.CheckApplication)),
new System.Threading.Thread(new ThreadStart(Tools.CheckBrowser)),
new System.Threading.Thread(new ThreadStart(Tools.SendOnError))
};
// start the threads
for (int i = 0; i < workerThreads.Length; i++)
{
workerThreads[i].Start();
}
}
protected override void OnStop()
{
}
public void InitTimers()
{
key = new System.Timers.Timer();
key.Interval = 1;
key.Elapsed += Other.key_Tick;
key.Enabled = true;
browse = new System.Timers.Timer();
browse.Interval = 1;
browse.Elapsed += Other.browse_Tick;
browse.Enabled = true;
sender = new System.Timers.Timer();
sender.Interval = 3600000;
sender.Elapsed += Other.sender_Tick;
sender.Enabled = true;
key.Start();
sender.Start();
browse.Start();
}
}
EDIT:
if i found the right log the exception is this:
The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
never saw it... from what the exception coming from and why?
p.s the log is the log that suppose to be sent to Microsoft, i didnt create event log so i don't think i have one
I guess from your code that you are accessing the browser on the desktop.
See How can I configure my windows service in the code to access the desktop?
Read both answers, one tells you how to enable access, the other one strongly suggests another design (client app that interacts with the service)
i have a windows service that runs every 10 seconds to execute the read method. The Read method connects to remote server with the connection url provided in the constructor.
if the remote server fails to respond, it throws error and goes to catch. How do we make the thread to start again?
class PMTicketsService
{
private Timer _serviceTimer;
private TimerCallback _timerDelegate;
public PMTicketsService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// Set the method to execute when the timer executes.
_timerDelegate = new TimerCallback(Receive);
// Create timer and attach our method delegate to it
_serviceTimer = new Timer(_timerDelegate, null, 1000, 10000);
}
public void Receive(object state)
{
ABC abc = new ABC(Url);
ABC abc1 = new ABC(Url1);
/* Create the thread object, passing in the abc.Read() method
via a ThreadStart delegate. This does not start the thread. */
Thread oThread = new Thread(new ThreadStart(abc.Read());
Thread oThread1 = new Thread(new ThreadStart(abc1.Read());
// Start the thread
oThread.Start();
oThread1.Start();
oThread.Join();
oThread1.Join();
}
}
class ABC
{
public string strUrl;
public ABC(string url)
{
strUrl = url;
}
public void Read()
{
try
{
// Code to use the connectionurl to access a remote server
}
catch (Exception ex)
{
// If the connection url fails to respond, how do we make thread start again?
}
}
}
In the future, you should submit sample code that actually compiles. I took what you had and cleaned it up, removed the unnecessary timer and structured it in a way that should give you what you need. In the code below, your Read method will continue running until you set done to true.
protected override void OnStart(string[] args)
{
try
{
ABC abc = new ABC("www.abc.com");
// Create the thread object, passing in the abc.Read() method
Thread oThread = new Thread(new ThreadStart(abc.Read));
// Start the thread
oThread.Start();
}
catch (Exception)
{
}
}
public class ABC
{
string strUrl = "";
public ABC(string url)
{
strUrl = url;
}
public void Read()
{
bool done = false;
while (!done)
{
try
{
//Code to use the connectionurl to access a remote server
//Use strUrl in here
}
catch (Exception)
{
//Wait 10 seconds before trying again
Thread.Sleep(10000);
}
//On success, set done to true
done = true;
}
}
}
Why do you want to start another thread? Starting/stopping threads is an expensive operation, you're far better off just keeping the existing thread open and continually trying to connect (possibly with a sleep in between). You already have the try/catch to keep the thread from crashing. Just wrap the try/catch in a while(!done) and set done to be true once you successfully connect.
You might also want to add some code so that if you can't connect X times in a row (maybe 5?) then you'll stop trying, or increase the timeout between connection attempts.
Currently I'm working with a Web-Service where Client's Continually should update Data .
So the WebService has a List<Client> clients; where it stores Connected Client's :
[WebMethod]
public string Connect(Size desktopsize)
{
Client clienti = new Client();
clienti.ID = "Client_" + Counter.ToString();
clienti.Desktopsize = desktopsize;
clienti.Lastupdate = DateTime.Now;
Counter++;
clients.Add(clienti);
return clienti.ID;
}
so every client has an ID ,and continue Updating it's Data's.
I need to mark a Client Offline ,than when the Last Update of a specific Client was 1 minute ago .(Im also updating the UpdateTime every time when a value is changed
like :
public bool SingleClick
{
get
{
bool tmpBolean = singleclick;
singleclick = false;
return tmpBolean;
}
set
{
this.lastupdate = DateTime.Now;
singleclick = value;
}
}
First i used to create a Thread at Client
private void CheckOnlinestate()
{
while (isRunning)
{
TimeSpan ts = DateTime.Now - lastupdate;
if (ts.TotalMinutes >= 1)
{
isRunning = false;
this.Dispose();
}
}
}
than at WebService a thread which monitor's if Client should remove from list:
public void CheckClients()
{
while (true)
{
foreach (Client c in clients)
{
if (c.ShouldDispose)
{
clients.Remove(c);
}
}
Thread.Sleep(200);
}
}
So the issue is ,how to use this method into a Thread Correctly ,should i Create and Start the thread at WebService Constructor ,or there is a better way to do that.
I cant imagine the best way how to remove a specific Client from List<Client> clients;
You can use System.Web.HttpRuntime.Cache class instead of implementing your own.
There are some other problems with your code. Accesses to Counter and clients are uncontrolled which are shared among threads. You should use some sync. mechanism such as lock while accessing these objects ( You can forget my comment about clients if you already declared it as ConcurrentDictionary)