Reputation: 826
I have this problem, I wrote a WinForms application, I have use it for a while with no problems. Next I just wrote a windows service to start/stop automatically my winforms app. But now the window is not shown, and in the task manager it's running. Besides, for testing my service, first it was a console app, so Console.WriteLine() was useful to display data, status, variables, etc, once it became a service, no console anymore. So, it's anything I'm missing?
Well, what I need is the service starting/stoping the winform application. It's a monitoring like. So, if the winform app is not running, creates a process, and starts it. But if is midnight, then the app should not be running, then the service finds the process and closes it. I don't want to turn the winform app into a service, or the service into a winform app.
I first thought just to put a shortcut to my winforms app in the "startup" folder to start it, but I need to close it, and/or restart it. So that's why I wrote the service.
Thanks!
Upvotes: 2
Views: 5244
Reputation: 73604
As of Windows Vista, a service can't interact directly with the desktop, meaning that it can't produce a visible form.
Even if you set it to try to do so, the best you will get is a UAC-style prompt indicating to the user than your program would like to be displayed. This was the result when we tried to follow this: How can a Windows service execute a GUI application?
We have one Windows app that absolutely needs to interact with the desktop it was originally written as a Windows Service. In order to get around the restrictions, we were forced to write it as a WinForms app with the Visible property of the main form set to "false". When we need to show it, we set it to "true". It's a hack and we know it, but we never could find a better solution.
This post does provide alternatives. They didn't work for us, but perhaps your situation is different. http://msdn.microsoft.com/en-us/library/windows/desktop/ms683502(v=vs.85).aspx
Upvotes: 2