2

I have a simple application that watches a folder for any changes as following:

            private void Form1_Load(object sender, EventArgs e)

>         {
>             FileSystemWatcher w = new FileSystemWatcher();
>             w.Path = @"C:\temp";
>             w.Changed += new FileSystemEventHandler(OnChanged);
>             w.Created += new FileSystemEventHandler(OnChanged);
>             w.Deleted += new FileSystemEventHandler(OnChanged);
>             w.Renamed += new RenamedEventHandler(OnChanged);
>             // Begin watching.
>             w.EnableRaisingEvents = true;
        }
    // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
        MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType + Path.GetFileName(e.FullPath));
    }

I added the same as service from the command prompt as

  sc create <service name> binPath= <path of the exe file>

This added the exe in the services and also made the entries in Registry. But when I tried to start the service as

sc start <service name>

it showed up the "Interactive Service Detection" message.

I want to avoid this message from popping up and start the service. I also need this to be done in c# but if anyone has any idea about doing it in cmd I can add it as a batch file and execute the same.

EDIT I

As @Seva suggested I created a service that calls the exe that I wish. I wrote the following code to start the exe on start of the service:

    protected override void OnStart(string[] args)
    {

        base.OnStart(args);
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerAsync();

    }
    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        p.StartInfo.CreateNoWindow = false;
        p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        p.StartInfo.WorkingDirectory = @"<my exe path>";
        p.StartInfo.FileName = "<myexe.exe>";
        p.StartInfo.Arguments = @"<my exe path>";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();
        p.WaitForExit();
        base.Stop();
    }

I installed the service successfully but is not starting the exe on starting.

EDIT II

The exe started. The service's property had to be configured to allow service interaction with desktop, but then again the "Interactive service detection" message is coming up.

1 Answers1

2

You will have to rearchitecture your windows service into two parts -- a GUI-less service process and a separate UI app that runs on user desktop. There are many ways service can communicate with UI app. These SO questions will get you started:

There is no other way around. BTW, your existing approach is already broken -- for Non-admin users and for remote desktop sessions -- they won't see UI from a service even if they want to.

Community
  • 1
  • 1
seva titov
  • 11,720
  • 2
  • 35
  • 54
  • Thank you @Seva Titov, I am actually a newbie to developing windows services. I just gave it a start by myself. Thank you for showing me a direction. I shall go through it right away. I just have this doubt: when a service calls this exe, wont it show up the message again? –  Jan 18 '13 at 06:21
  • I created this service and this service calls the exe, but when I tried this the exe was not executing [As in EDIT I]. But when I checked the service's property, in the Log On tab "Allow Service To interact With Desktop" was disabled. And when I tried again the "Interactive service detection" message popup comes again. –  Jan 18 '13 at 11:12
  • Normally a user mode application calls into a service, rather than service calls to the user mode app. The reason is there could be multiple users logged on the computer (or none at all), and service has to have an awareness on where they all are and what state they are in. Typical example: user mode app registers a tray icon and communicates to the service; when user app determined that some action is required from user, it displays a balloon or opens up a new window to attract user attention. – seva titov Jan 18 '13 at 15:35
  • Hi Seva, I have found same issues but in jenkins. How can I see real browser on system? I do not want to run my Jenkins job in Interactive Service Detection. – Sagar007 Nov 26 '15 at 07:59