开发者

Check if I can show a dialog (based on current code is running in a winform app OR windows service)

I have some shared code I need to use both in a WinForm and a service. How can I test if my code is running in a service, so I开发者_运维知识库 can avoid problems with modal dialogs.


It might be enough for you to test the Environment.UserInteractive property, and only show the dialog if this is true.


  1. System.Environment.UserName is SYSTEM or Network then most possibly it is a service.

  2. Check callstack and see if very first method called belongs to the class derived from ServiceBase

  3. You could also check Process.SessionId to be 0 because all system processes & services run under session 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜