How to launch a Winforms app from a windows-servic

2019-08-21 20:12发布

问题:

Hello i am trying to launch an interactive process - a winforms app - from a windows service on the current session. I do not know how to do that.I am using TopShelf and i tried launching the process using the WhenSessionChanged hook provided by TopShelf. I can get the sessionId

 var exitCode = HostFactory.Run(x => {
                    x.Service<MyService>(s => {
                        s.ConstructUsing((h) => new MyService());
                        s.WhenStarted(t => t.Run());
                        s.WhenStopped(t => t.Stop());
                        s.WhenSessionChanged((anubis, args) => {
                              string winformsPath="D://WinFormsApp/Wf.exe"
                              int sessionId = args.SessionId;
                              Process process = new Process();
                              process.StartInfo = new ProcessStartInfo(winformsPath);
                              process.Start();
                         });
                    });
                    x.RunAsLocalSystem();
                    x.SetServiceName(Constants.ISO.NAME);
                    x.SetDisplayName(Constants.ISO.DISPLAY);
                    x.SetDescription(Constants.ISO.DESCRIPTION);
                });

I do not know what to do with the sessionId.
P.S
I did not post the definition of MyService because it is not important in our case.I just want to launch the winforms on the current session.