如何在Windows服务启动一个进程时,计时器事件引发?计时器、进程、事件、如何在

2023-09-02 01:22:49 作者:时尚好听的QQ

我创建了一个Windows服务与计时器,并在发射 timer.Elapsed 我创建一个进程(的System.Diagnostics.Process的事件。启动(EXE路径))以5秒的时间间隔。但是,这个过程没有得到一个事件的触发产生。

是否有这样做的任何其他方式?

在此先感谢。

 私人无效timer_Elapsed(对象发件人,System.Timers.ElapsedEventArgs E)
{

    流程PR =新工艺();
    pr.StartInfo.FileName = @C: Program Files文件信使 msmsgs.exe;
    pr.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    pr.StartInfo.CreateNoWindow = FALSE;
    pr.Start();
}
 

解决方案

您要添加注释中的问题的描述,你走的本来知道在一开始就乐于助人。你可以看到这个过程的原因已经开始,但没有窗户永远被打开是因为在Windows Vista和更高版本的Windows服务所做的安全模式更改。您还没有提到它特别,但我有一个强烈的怀疑,你想这些操作系统之一下运行这一点。真正的问题是不启动一个进程,它显示的UI。

你要完成什么叫做互动服务,该是一个被允许直接与用户和桌面交互(例如,显示一个窗口或对话框)。 如果你使用的是Windows XP(和你的服务只的永远要在Windows XP下运行),可以按照该文章中的说明,使您的服务交互模式运行,这将让你像您期望的显示使用Adobe Acrobat应用程序窗口。然而,由于该文件还显示,该功能的不能正常工作的在Windows Vista和更高版本:

  我的电脑里没有windows installer这个服务怎么办

重要 服务不能直接与用户的Windows Vista交互。   因此,在部分中提到的技术,标题为使用交互式服务   应在新的code不被使用。

更具体地,在这些版本,更改了服务和应用程序是如何运行进行。服务现在分离系统在会话0中,而应用程序在其他会话中运行。这是为了从应用程序code,它发起的攻击孤立的服务。因此,由服务表示没有用户界面将永远不会看到系统上的任何用户,其中包括一个简单的消息框。您可以阅读白皮书这里解释了这些变化,更详细的。如果你是一个更直观的人,请参考下图说明了新的模式,应特别注意分割线:

     

其结果是,如果您的目标的版本,也可能永远需要针对那些版本中,你不能利用这个漏洞。我说的漏洞,因为底线,正如我在评论中提到,是 Windows服务是不打算成为交互式。什么你想在这里做有悖于服务的目的和设计。这不是建议过,而现在它平行不通的。如果你需要这个特殊的功能,你应该创建一个不同类型的应用程序。 Windows窗体和WPF旨在为用户模式应用程序,都可以启动程序,必要时打开新窗口。我强烈建议你转换为这种风格的应用程序,而不是。

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();
}

解决方案

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:

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.