问题
I am working on something similar to this question asked in 2010
How to call Windows application using Windows service in C#?
This answer might be the solution to my problem
https://stackoverflow.com/a/2309089/1270384
Although I don't know how to do that.
Was wondering if somebody could give me an example of what is being mentioned or something similar, namely the process class being mentioned or just a brief example of how to go about the instructions given.
Update
I have a web application that checks my database for changes to a particular table. I'd like my application to be called inside of this windows service that I am trying to create which I'd like to schedule to run every 20seconds.
I'm new to windows services so didn't quite get what was being explained.
Any clarification would be greatly appreciated.
回答1:
In the services control panel, on the Log On tab, check the "Allow Service to interact with Desktop" check box. then your can do something like this.
public class WinService : ServiceBase
{
Process p = new Process();
public WinService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
RunApp();
}
private void RunApp()
{
p.StartInfo.FileName = @"<path to your app>";
p.StartInfo.Arguments = "<your params>";
p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
p.Start();
}
protected override void OnStop()
{
p.Kill();
}
}
EDIT: parameters passing:
List all the parameters in startInfo.Arguments, separate them by blanks. Numeric parameters are listed as is, string paarameters are listed in quotes.
Example:
If the command line for your applpication is:
YourApp.exe param1 "param two" param3
then your startInfo.Arguments should be set to:
startInfo.Arguments = "param1 \"param two\" param3";
回答2:
You could always encapsulate the logic in your web application in a separate class that you can simply invoke in your service.
Else you can call your web site by using the WebClient
class.
来源:https://stackoverflow.com/questions/10309426/call-windows-application-in-windows-service