如果已在运行,如何将参数传递给控制台应用程序?
-
16-09-2020 - |
题
我在Windows Mobile中使用控制台应用来处理传入的消息拦截。在相同的控制台应用程序I接受基于参数的参数(String args []),注册消息拦截器。
拦截点是一个枚举
static void Main(string[] args)
{
if (args[0] == "Location")
{
addInterception(InterceptorType.Location, args[1],args[2]);
}
}
private static void addInterception(InterceptorType type, string Location, string Number )
{
if (type == InterceptorType.Location)
{
using (MessageInterceptor interceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete, false))
{
interceptor.MessageCondition = new MessageCondition(MessageProperty.Sender, MessagePropertyComparisonType.Contains, Number, false);
string myAppPath = Assembly.GetExecutingAssembly().GetName().CodeBase;
interceptor.EnableApplicationLauncher("Location", myAppPath);
interceptor.MessageReceived += new MessageInterceptorEventHandler(interceptor_MessageReceived);
}
}
}
static void interceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
{
//Do something
}
.
我制作了一个控制台应用程序,因为我希望它继续在背景中运行并拦截传入的消息。
这首次工作正常。但问题是我必须继续调用addinterception方法来添加随后的拦截规则。这使得控制台应用程序再次启动,每次添加规则时都会重新开始。我如何只使这个运行一次并添加更多消息拦截器规则?
解决方案
由于您已经有一个方法来调用命令提示一次,请使用一些简单的循环更新您的逻辑,以便通过n命令。
编辑:我写了一个完全可编译的例子来告诉你我谈论的是什么。请注意,在不重新启动的情况下,如何调用子进程的任何次数。这不仅仅是一个简单的命令行启动,因为该想法将导致X进程,这正是您不想要的。
父进程:(具有system.diagnostics.process)
/// <summary>
/// This is the calling application. The one where u currently have System.Diagnostics.Process
/// </summary>
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Process p = new Process();
p.StartInfo.CreateNoWindow = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = @"C:\AppfolderThing\ConsoleApplication1.exe";
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("Output received from application: {0}", e.Data);
};
p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e)
{
Console.WriteLine("Output received from application: {0}", e.Data);
};
p.BeginErrorReadLine();
p.BeginOutputReadLine();
StreamWriter inputStream = p.StandardInput;
inputStream.WriteLine(1);
inputStream.WriteLine(2);
inputStream.WriteLine(-1);//tell it to exit
p.WaitForExit();
}
}
.
子进程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
enum InterceptorType
{
foo,
bar,
zee,
brah
}
/// <summary>
/// This is the child process called by System.Diagnostics.Process
/// </summary>
class Program
{
public static void Main()
{
while (true)
{
int command = int.Parse(Console.ReadLine());
if (command == -1)
Environment.Exit(0);
else
addInterception((InterceptorType)command, "some location", "0");
}
}
private static void addInterception(InterceptorType type, string Location, string Number)
{
switch (type)
{
case InterceptorType.foo: Console.WriteLine("bind foo"); break;
case InterceptorType.bar: Console.WriteLine("bind bar"); break;
default: Console.WriteLine("default bind zee"); break;
}
}
static void interceptor_MessageReceived(object sender, EventArgs e)
{
//Do something
}
}
}
.
注意,Codeplex有一个托管服务库。
其他提示
编辑
人们似乎都misunterstanding您的问题(或我),所以这里的一些澄清如何我看到的问题。
您有一个获取命令行参数的控制台应用程序。这些参数用于某些东西(实际上是无关的)。通过使用新命令行args调用应用程序,在应用程序已经运行后,您希望能够添加参数。
发生了什么是,当您首先在teh之后随时调用应用程序时,该进程的新实例启动而不是已运行应用程序的命令行参数。
结束编辑
解决方案相当简单,需要两件。
-
您需要一个名为互斥锁的。无论如何(差)原因,CF都不暴露一个互斥锁的版本,其中您必须 p / Invoke createmutex 或使用已经拥有它的库(如SDF)。您的应用程序需要在启动时创建互斥锁并检查它是否已存在。如果它不是第一个运行实例并运行正常。如果存在互斥锁,则需要将命令行args传递给已通过 p2p队列然后简单地退出。
-
检查互斥锁后,第一个实例会产生一个工作线程。此线程在P2P队列上侦听消息。当他们进来时,你处理它们。