コンソールアプリケーションに引数を渡す方法すでに実行されている場合は?
-
16-09-2020 - |
質問
着信メッセージの傍受を処理するために、Windows Mobileでコンソールアプリケーションを使用します。同じコンソールアプリケーションで、パラメータに基づいてパラメータ(文字列args [])を受け入れ、メッセージインターセプターを登録します。
インターセプタ型はenum
です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メソッドを呼び出し続ける必要があるということです。これにより、コンソールアプリケーションはルールを追加するたびに再度実行されます。これを一度だけ実行してメッセージインターセプタールールを追加するにはどうすればよいですか?
解決
コマンドプロンプトを1回呼び出すためにすでにメソッドを持っているので、nコマンドを渡すことができます。
編集:私はあなたに話しているものを正確に示すために完全にコンパイル可能な例を書きました。再起動することなく、子プロセスを任意の回数と呼ぶことができる方法に注意してください。これは単純なコマンドラインの起動ではなく、その考えがXプロセスにつながるため、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には、管理サービスライブラリ
があります。他のヒント
編集
人々はあなたの質問を誤解しているようです(または私は)ので、私がどのように問題を見ているかについてのいくつかの説明です。
コマンドラインパラメータを取り込むコンソールアプリを持っています。これらのパラメータは何かに使用されます(実際には無関係なもの)。新しいコマンドラインargsを使用してアプリを呼び出してアプリが既に実行されている後にパラメータを追加できるようにしたいです。
最初にTEHの後にいつでもアプリを呼び出すと、すでにアプリケーションを実行しているコマンドライン引数の代わりに、プロセスの新しいインスタンスが起動します。
終了
解決策はかなり簡単で、2つの部分を必要としています。
-
あなたは mutexという名前のが必要です。どんな(悪い)の理由でも、CFは名前を取り込むバージョンのミューテックスを公開しないので、 p / createmutex を呼び出すか、すでに持っているライブラリ(SDFのように)を使用してください。アプリアプリは起動時にミューテックスを作成する必要があり、すでに存在するかどうかを確認します。それがあなたが最初に実行されていて通常どおりに実行されない場合。ミューテックスが存在する場合は、コマンドラインargsをa