0

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.

Community
  • 1
  • 1
user1270384
  • 711
  • 7
  • 24
  • 53
  • The Process Class info can be found [here](http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx) – Mark Hall Apr 25 '12 at 04:51
  • I've placed an update with background on what I am trying to accomplish. Would this be possible using the process class from the link you have given? – user1270384 Apr 25 '12 at 04:57
  • `I have a web application that checks my database for changes to a particular table` Do your logic inside windows service instead of calling the web application from windows service. – Waqas Raja Apr 25 '12 at 08:16

2 Answers2

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";
Flot2011
  • 4,601
  • 3
  • 44
  • 61
  • You mean I can actually call my Web project the way you have just explained above in p.StartInfo.Filename=@""? Is that correct? – user1270384 Apr 25 '12 at 05:02
  • I answered before you edited your question so I didn't have Web applications in mind, but theoretically I would think so. – Flot2011 Apr 25 '12 at 05:09
  • What exactly do you mean 'Services control panel'? Are you talking about in the MMC? the Service designer in visual studio? I have looked in both and have found no such tab? Oh, this must be for windows XP. I am guessing this feature has been taken out of later versions of Windows. UPDATE: I stand corrected. It is indeed in windows 7. Never mind :) – SoftwareSavant Oct 06 '15 at 12:31
0

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.

Eben Roux
  • 12,983
  • 2
  • 27
  • 48
  • Thats exaclty what the first comment said but nobody is saying how to do that. I tried to create a new class with my sqlconnections and everything in it but it says: Invalid token '(' in class, struct, or interface member declaration – user1270384 Apr 26 '12 at 22:35