Search code examples
c#windows-servicesservicecontrollersystray

How to get answer from windows service


I´ve got a windows service and a NotifyIcon-Applicaiton (Sys-Tray) which should communicate with the service. The App should send a command and receive a PORT from the service for a socket which provides the further communication.

The Service overrides the OnCustomCommand Method but I don´t know how to send an answer to the calling-programm.

    protected override void OnCustomCommand(int command)
    {
        switch (command)
        {
            case 'p':
                // SEND Port number
                break;
            case 's':
                // SEND Service Name
                break;
            case 'n':
                // SEND Notification
                break;
        }

        base.OnCustomCommand(command);
    }

Notify-Icon-Application

var serviceController = new ServiceController("ATLED Service"); serviceController.ExecuteCommand((int)'p');


Solution

  • There can be different ways you can do that

    1. You can use a TCP port, which is listening for commands reply from server
    2. You can use signalR on the app side and connect service and app using that
    3. You can also use share memory or may be a file which is create/written by service when something is there for app, or can use a database as well