2

Windows Mobileのコンソールアプリケーションを使用して、着信メッセージの傍受を処理します。同じコンソールアプリケーションで、パラメーター(string args [])を受け入れ、パラメーターに基づいてメッセージインターセプターを登録します。

InterceptorTypeは列挙型です

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回だけ実行し、メッセージインターセプタールールを追加するにはどうすればよいですか?

4

2 に答える 2

3

コマンドプロンプトを一度呼び出すメソッドがすでに用意されているので、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にはマネージドサービスライブラリがあることに注意してください。

于 2010-07-15T16:13:18.793 に答える
2

編集

人々はあなたの質問を誤解しているようです(または私はそうです)ので、ここに私が問題をどのように見ているかについてのいくつかの説明があります。

コマンドラインパラメーターを取り込むコンソールアプリがあります。これらのパラメーターは、何か(実際には無関係なもの)に使用されます。新しいコマンドライン引数を使用してアプリを呼び出すことにより、アプリの実行後にパラメーターを追加できるようにする必要があります。

何が起こっているのかというと、最初にアプリを呼び出すと、コマンドライン引数が既存の実行中のアプリケーションに送られる代わりに、プロセスの新しいインスタンスが起動します。

編集終了

解決策はかなり簡単で、2つの部分が必要です。

  1. 名前付きミューテックスが必要です。何らかの(悪い)理由で、CFは名前をとるミューテックスのバージョンを公開しないため、CreateMutexをP /呼び出しするか、すでにそれを持っているライブラリ(SDFなど)を使用する必要があります。アプリは起動時にミューテックスを作成し、それがすでに存在するかどうかを確認する必要があります。そうでない場合は、最初に実行するインスタンスであり、通常どおり実行します。ミューテックスが存在する場合は、コマンドライン引数をP2Pキューを介してすでに実行されているものに渡す必要があります。その後、単に終了します。

  2. ミューテックスをチェックした後、最初のインスタンスはワーカースレッドを生成します。このスレッドは、P2Pキューでメッセージをリッスンします。彼らが入ってくるとき、あなたは彼らを扱います。

于 2010-07-15T16:00:03.190 に答える