I've been searching all over the internet whole the last night. This question has been answered so many times but a simple answer is never given. If you have the same question there is a very simple but a tricky way to do something when the service asks for it.
Let's say you want to play a song when service starts.
first of all create an EventLog class:
public class EventLogEngine
{
private string _sourceName, _logName;
public EventLogEngine(string sourceName, string logName)
{
if (!EventLog.SourceExists(sourceName))
EventLog.CreateEventSource(sourceName, logName);
_sourceName = sourceName; _logName = logName;
}
public void WriteLog(string message, EventLogEntryType eventType, int Id)
{
EventLog.WriteEntry(_sourceName, message, eventType, Id);
}
}
protected override void OnStart(string[] args)
{
EventLogEngine eventWriter = new EventLogEngine("mySource","myLog");
eventWriter.WriteLog("sourceName","Service started",EventLogEntryType.Information,"anyId");
}
There is nothig about playing a sound till here and there is nothing complicated yet. But how to play a sound or do something else? Here is the answer :)
1-Go to control panel and open Event Viewer
2-Find your event log and click on it
3-On the right panel you will see your entries which you have write through your code.
4-Right click on the entry and select Attach Task To This Event!
So far you should have understood what I am going to do. right?
5-Select that option and declare what do you want to do when this entry is set. You can simply attach as many as tasks you wish.
Now write a program that plays a sound (Ex:in vb6) and tell Event Viewer to carry out this program each time this entry gets written(each time your service starts)