Question

I have a simple service with the following code:

on Program.Main method I have the code which is generated by vs itself(2010):

static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service1() 
        };
        ServiceBase.Run(ServicesToRun);
    }

And in Service1.cs I have:

 protected override void OnStart(string[] args)
    {
        System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(@"C:\doorbell-1.wav");
        myPlayer.Play();
    }

    protected override void OnStop()
    {
    }

I have omit writing the usual automatically generated c# codes to reduce the complexity.

Logically a sound should be played when I start the service but nothing happens when I start the service. Please note that:

1-I install the service using installUtill.exe. 2-The service runs under the localSystem account privilege. 3-Duration of the mentioned .wav file is 3Seconds.

How can I play that sound? Thanks in advance.

Was it helpful?

Solution 2

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

enter image description here

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!

enter image description here

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.

enter image description here

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)

OTHER TIPS

The simple answer is that you can't. Windows Services do not interact with the desktop, and thus they are not capable of using desktop functions like audio services.

Remember, Windows is a multi-user operating system. Can you imagine what would happen if 5 simultaneously logged in users started playing audio?

All users have what's known as a "Windows Station", and there is a special Windows Station for the user logged into the physical computer. Windows Services have a null or unique (non-interactive) Windows Station, and thus cannot interact with the console WS.

This Windows Station is what is used to redirect audio, and the audio in the console WS goes to the speakers. All other audio either is redirected to the network station they're using, or does nothing.

A more complex answer is that it might be possible, since the Windows Audio service is itself another service, and you might be able to interact with it directly, but this would be very low level and something you are probably not skilled enough to do.

Finally, it's possible to make services interact with the desktop. However, this is considered a deprecated function and is not always easy to use. It's also a huge security vulnerability, and makes your service susceptible to being used by malware to compromise a machine.

It is possible to play sounds from a windows service.

Play wave file from a Windows Service (C#)

Playing a sound from a service doesn't violate the rules of interactivity because it's not an interactive function: it's a notification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top