引发 Timer 事件时,Windows 服务如何启动进程? [英] How can a Windows Service start a process when a Timer event is raised?

查看:20
本文介绍了引发 Timer 事件时,Windows 服务如何启动进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个带有计时器的 Windows 服务,并且在 timer.Elapsed 的触发事件中我正在创建一个进程 (System.Diagnostics.Process.Start(exe path)) 间隔 5 秒.但是这个过程不会在事件触发时创建.

有没有其他方法可以做到这一点?

提前致谢.

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

解决方案

您正在评论中添加问题的描述,因为您在开始时会有所帮助.您可以看到进程已启动但从未打开任何窗口的原因是因为在 Windows Vista 及更高版本下对 Windows 服务进行了安全模型更改.您还没有具体提到它,但我强烈怀疑您正试图在其中一种操作系统下运行它.真正的问题不是启动进程,而是显示 UI.

您要完成的任务称为交互式服务",它允许直接与用户和桌面交互(即显示窗口或对话框).
如果您使用的是 Windows XP(并且您的服务只会永远 必须在 Windows XP 下运行),您可以按照该文章中的说明使您的服务以交互模式运行,这将允许您按预期显示 Adob​​e Acrobat 应用程序窗口.但是,该文档还表明,此功能在 Windows Vista 及更高版本下不起作用:

<块引用>

重要  从 Windows Vista 开始,服务无法直接与用户交互.因此,标题为使用交互式服务的部分中提到的技术不应在新代码中使用.

更具体地说,在这些版本中,对服务和应用程序的运行方式进行了更改.服务现在在会话 0 中被系统隔离,而应用程序在其他会话中运行.这旨在将服务与源自应用程序代码的攻击隔离开来.因此,服务显示的任何 UI 都不会对系统上的任何用户可见,包括一个简单的消息框.您可以在此处阅读更详细地解释这些更改的白皮书.如果您是一个更直观的人,请参阅下图说明新模型,特别注意分界线:

    

结果是,如果您以这些版本为目标,或者可能需要以这些版本为目标,则无法利用此漏洞.我说漏洞"是因为正如我在评论中提到的,底线是 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.

这篇关于引发 Timer 事件时,Windows 服务如何启动进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆