How can a Windows Service start a process when a Timer event is raised?
Asked Answered
D

4

8

I have created a Windows Service with Timer and in firing event of timer.Elapsed I am creating a process (System.Diagnostics.Process.Start(exe path)) at interval of 5 seconds. But this process does not get created on the firing of an event.

Is there any other way of doing this?

Thanks in advance.

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{       

    Process pr = new Process();
    pr.StartInfo.FileName = @"C:\Program Files\Messenger\msmsgs.exe";
    pr.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    pr.StartInfo.CreateNoWindow = false;
    pr.Start();
}
Donets answered 23/12, 2010 at 5:46 Comment(8)
Or it is created and then exited immediately? If you can paste the code here, it would be helpfulAudie
Windows Services are not designed to run interactive applications. You can execute a process, but getting a new window to open is a dicey proposition. Not to mention all of the security issues that you'll run into trying to do this. Chances are, you need to rethink your design and use something other than a Windows Service, especially if your target users are running Windows Vista or later considering its tightened security model for services.Diapositive
Also, see possible duplicate here: #678374 (discussing how you can elevate the service to run with different credentials and/or in interactive mode (note: this is not supported under Vista or later)).Diapositive
thanks Cody, Can u tell me what else can be done to execute an .exe file from the timer's Event??Donets
The idea is you can't... I don't know what was unclear about my previous explanation.Diapositive
Did you try to make the service interactive? Also check with task manager whether the process is launching but not being displayed. msdn.microsoft.com/library/default.asp?url=/library/en-us/…Ferguson
in task manager, process is running but it doesnt get opened.Donets
This worked in June 2019 in Visual Studio with a .NET 4.7.2 application: code.msdn.microsoft.com/windowsapps/…Skillful
D
21

You're adding descriptions of the problem in the comments as you go along that would have been helpful to know at the outset. The reason that you can see the process has started but no window ever gets opened is because of security model changes that were made to Windows Services under Windows Vista and later. You haven't mentioned it specifically yet, but I have a strong suspicion that you're trying to run this under one of those operating systems. The real issue is not starting a process, it's showing a UI.

What you're trying to accomplish is called an "Interactive Service", which is one that is allowed to interact directly with the user and the desktop (i.e., show a window or dialog).
If you're using Windows XP (and your service will only ever have to run under Windows XP), you can follow the instructions in that article to enable your service to run in interactive mode, which will allow you to display the Adobe Acrobat application window as you expect. However, as that documentation also indicates, this feature does not work under Windows Vista and later:

Important  Services cannot directly interact with a user as of Windows Vista. Therefore, the techniques mentioned in the section titled Using an Interactive Service should not be used in new code.

More specifically, in those versions, changes were made to how services and applications are run. Services are now isolated by the system in Session 0, while applications are run in other sessions. This is intended to isolate services from attacks that originate in application code. Hence, no UI shown by a service will ever be visible to any user on the system, including a simple message box. You can read the white paper that explains these changes in more detail here. If you're a more visual person, refer to following diagram illustrating the new model, paying specific attention to the dividing lines:

     Windows process isolation model under Windows Vista/7

The upshot is that if you're targeting those versions, or might ever need to target those versions, you can't use this loophole. I say "loophole" because the bottom line, as I mentioned in a comment, is that Windows Services are not intended to be interactive. What you're trying to do here runs contrary to the purpose and design of a service. It wasn't recommended before, and now it flat doesn't work at all. If you need this particular functionality, you should create a different kind of application. Both Windows Forms and WPF are intended for user-mode applications, and both can start processes and open new windows as necessary. I strongly recommend that you convert to this style of application instead.

Diapositive answered 23/12, 2010 at 6:54 Comment(7)
If there any method to impletement GlobalKeyHook via windows service ? and also service can look the keypress events SetWindowsHookEx ?Scarecrow
@Jimmer No. Global Keyboard Hook from windows serviceDiapositive
ok but what if I intend to run an exe daily? I would have created a win service which will fire it daily. To create another desktop app to invoke exe daily defeats the purpose of automation.Accountancy
Windows Services shouldn't be running standard executables, @ahsant. Create a standard application that runs a timer and launches your desktop app. No need for a service, and you get all the same benefits of automation.Diapositive
Thanks Cody for getting back to this old thread. The problem with standard app is the moment I log off they will die as well. Am I missing something? so lets say I create a console app, it will have a timer and it will fire my exe, but will it still work when I am logged off??Accountancy
@Accountancy Ah, you want it to run when there's no account logged in. Okay, so, what account context should it run under when there's no user logged in? Oops, there isn't one. There's also no user desktop on which it can display a UI. That's why you shouldn't display desktop/UI app from a Windows Service. Why can't you factor out the parts of the code that need to run in the background, and put those in the Service? Leave the UI parts in a separate desktop app that can display a UI. It need not persist across logoffs. If necessary (e.g., to a make a monitor), have it communicate with the Service.Diapositive
@Cody, yes that is the problem, I am working with this third party app. I dont have code of it, cant refactor it. It updates some files when you run it. I was hoping to make it automated so I dont have to log in to server daily and run it manually. Thanks for your feedback anyway.Accountancy
S
1

Since you won't get a process to show UI from the service, I recommend that you run the helper process when the user logs on and start your process from it.

Singband answered 23/12, 2010 at 8:6 Comment(2)
I don't. At least not necessarily... Why create a service and a helper process? If you find yourself needing to create a helper process that autostarts on login (blech), then you need to seriously re-evaluate whether you need a service in the first place. Just because you can do something doesn't mean that it's recommended you do it.Diapositive
Yeah - you are right on this one. However, if you need something that will run ALWAYS and report to the user SOMETIMES then this architecture is kind of OK-ish?Iconoclasm
S
1

I realize I'm a little late to this party. But since this question came up in my search for a solution, I'll provide an answer to the OP's question.

You can start a program from a service using CreateProcessAsUser, using the token of the logged-on user. MSDN provides a nice sample app demonstrating this; look for CSCreateProcessAsUserFromService: https://code.msdn.microsoft.com/windowsapps/CSCreateProcessAsUserFromSe-b682134e#content

I tried it, and it works. I'm able to create a new process for my existing .NET application from a service, and it works just fine. I set the service Log On properties to Local System account, but left the "Allow service to interact with desktop" unchecked. My application is created when the timer expires, and works just like it does when started normally, including GUI presentation and user interaction.

Whether this is an improper or undesirable thing to do is another discussion. But it is indeed possible, and really not all that difficult.

Senter answered 10/7, 2015 at 15:8 Comment(1)
link does not work - Perhaps you could show a snippet of the solution herePirali
B
1

There is a kind of work around, but i would advice to only do this if YOU ARE SURE TO ACCEPT THE SECURITY ISSUE!

This could be the Answer to your Problem: CreateProcessAsUser.

The sample demonstrates how to create/launch a process interactively in the session of the logged-on user from a service application written in C#.Net.

Beat answered 12/8, 2015 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.