在Windows服务中调用windows应用程序 [英] Call windows application in windows service

查看:523
本文介绍了在Windows服务中调用windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理与2010年问题类似的问题



如何在C#中使用Windows服务调用Windows应用程序?



这个答案可能是我的问题的解决方案



http://stackoverflow.com/a/2309089/1270384



虽然我不知道该怎么做。



想知道是否有人可以给我一个正在提及的东西或类似东西的例子,即提到的过程类或者一个简单的例子,如何去指示。



更新



我有一个网络应用程序,一个特定的表。我想我的应用程序在这个Windows服务内部,我想创建,我想安排运行每20秒。



我刚接触Windows服务,所以没有得到解释。



任何澄清都将非常感激。

解决方案

控制面板,在登录选项卡上,选中允许服务与桌面交互复选框。那么你可以这样做。

  public class WinService:ServiceBase 
{
Process p = new处理();

public WinService()
{
InitializeComponent();
}

protected override void OnStart(string [] args)
{
RunApp();
}

private void RunApp()
{
p.StartInfo.FileName = @<您的应用程序的路径>;
p.StartInfo.Arguments =< your params>;
p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
p.Start();
}

protected override void OnStop()
{
p.Kill();
}
}

EDIT:参数传递:



列出startInfo.Arguments中的所有参数,用空格分隔它们。
数字参数按原样列出,字符串参数以引号形式列出。



示例:



如果您的应用程序的命令行是:

  YourApp.exe param1param twoparam3 

那么你的startInfo.Arguments应该设置为:

  startInfo.Arguments =param1 \param two \param3; 


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

http://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.

解决方案

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";

这篇关于在Windows服务中调用windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆