If you have some knowledge about Windows services and their functionalities then go ahead. Otherwise you may not understand some of the points mentioned here.
Are you suffering from starting a process from your windows service ? I have a cunning solution for that. Normally we cannot start any process either on our local machine or from a remote machine through a windows service.
We can start the process by enabling the Desktop Interactive of the service, but the UAC system of Vista and Windows 7 is a problem, that every time we start the process from a service it asks whether to allow the desktop interactivity. We can’t ask our clients to stop the UAC system. So what is the solution ?
Now I think you have some idea why can’t we start a process from service. But you might have a question in your mind why we need a process for a service ? There can be several answers
- We need to perform a CPU intensive operation
- COM interoperability is clashing with some threading components of the service
- Some actions can be performed more efficiently by a separate process rather than a service
I figured out this solution as I suffered from the bolded point. Here the method I have used.
Create an ASPX webpage (it should be hosted in a later version of IIS 4.0)
In the Page_Load method start the process.
Then Create the Windows Service to access the the web page. (Access it through normal WebRequest and WebResponse). Cool !
The real cool thing is you can almost start all the processes by using this method from a windows service. Even processes with arguments and Verbs.’
Here’s the code for the webpage
protected void Page_Load(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = "path to your process (your .exe file)"
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
p.Dispose();
}
In the service within the OnStart() put the following code
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("ASPX page path");
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
}
catch (Exception ex)
{
}