Windows Service与桌面属性问题交互 [英] Windows Service Interact with desktop property problem

查看:143
本文介绍了Windows Service与桌面属性问题交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经使用C#开发了Windows服务.我从Windows服务代码中显示了几个消息框,因此我为服务设置了与桌面交互"属性.这是使用以下代码完成的:(顺便说一句,我认为这是我仅从Code Project获得的代码)

Hi All,

I have developed a windows service using C#. I display a couple of message boxes from my windows service code and hence I set the "Interact with desktop" property for my service. This is done using the following code: (By the way I think this was code I got from Code Project only)

try
     {
                
        ConnectionOptions coOptions = new ConnectionOptions();
        coOptions.Impersonation = ImpersonationLevel.Impersonate;
              
        ManagementScope mgmtScope = new ManagementScope(@"root\CIMV2", coOptions);
        mgmtScope.Connect();
        ManagementObject wmiService;
        wmiService = new ManagementObject("Win32_Service.Name=''" + "My Service" + "''");
        ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
        InParam["DesktopInteract"] = true;
        wmiService.InvokeMethod("Change", InParam, null);       
      }
      catch (Exception ex)
      {
             
      }



我面临的问题是,尽管为我的服务设置了与桌面交互"属性,但是在尝试显示消息框时出现以下错误:

System.InvalidOperationException:当应用程序不在UserInteractive模式下运行时,显示模式对话框或窗体是无效操作.指定ServiceNotification或DefaultDesktopOnly样式以显示来自服务应用程序的通知.
在System.Windows.Forms.MessageBox.ShowCore处(IWin32Window所有者,字符串文本,字符串标题,MessageBoxButtons按钮,MessageBoxIcon图标,MessageBoxDefaultButton defaultButton,MessageBoxOptions选项,布尔showHelp)
在System.Windows.Forms.MessageBox.Show处(字符串文本,字符串标题,MessageBoxButtons按钮)


该属性似乎在某些时候起作用.但是大多数情况下,它会失败并导致服务停止.
任何人都可以在这里帮助我.请告诉我我要去哪里错了?

在此先感谢
Lydia



The problem I am facing is that inspite of setting the "interact with desktop" property for my service I am getting the following error when I try to display a message box:

System.InvalidOperationException: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.
at System.Windows.Forms.MessageBox.ShowCore(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, Boolean showHelp)
at System.Windows.Forms.MessageBox.Show(String text, String caption, MessageBoxButtons buttons)


This property seems to work sometime. But most of the times it is failing and causing the service to stop.
Can anyone please help me out here. Please tell me where I am going wrong?

Thanks in advance
Lydia

推荐答案

服务无法显示消息框,并且Vista和Windows 7都不允许/不支持与桌面交互"(XP和XP中Windows服务的功能)年龄较大).您要做的是创建一个可以与服务通信的系统任务栏应用程序,并让该应用程序显示服务发送的通知.
A service cannot display a messagebox, and neither Vista and Windows 7 do not allow/support "interacts with desktop" ( a feature of windows services in XP and older). What you want to do is create a system tray application that can communicate with the service, and have that app display notifications sent by the service.


这篇关于Windows Service与桌面属性问题交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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