Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

c# - How can a Windows Service start a process when a Timer event is raised?

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 FilesMessengermsmsgs.exe";
    pr.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    pr.StartInfo.CreateNoWindow = false;
    pr.Start();
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...