如果控制器应用程序已经运行,如何将参数传递给它?

我在Windows Mobile中使用控制台应用程序来处理传入的消息拦截。在同一控制台应用程序中,我接受基于参数的参数(字符串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方法来添加后续的拦截规则。这使得控制台应用程序每次添加规则时都会反复启动。我如何只运行一次并添加更多的消息拦截器规则?     
已邀请:
由于您已经有一个方法来调用命令提示符一次,因此可以通过一些简单的循环更新逻辑,以便传递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:AppfolderThingConsoleApplication1.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调用应用程序来添加参数。 发生的事情是,当您在第一次之后的任何时间调用应用程序时,将启动该进程的新实例,而不是将命令行参数发送到现有的已在运行的应用程序。 结束编辑 解决方案相当简单,需要两个部分。 您需要一个命名的互斥锁。对于任何(不良)原因,CF不会公开带有名称的互斥体版本,因此您必须使用P / Invoke CreateMutex或使用已经拥有它的库(如SDF)。您的应用需要在启动时创建互斥锁,并检查它是否已存在。如果它不是你是第一个正在运行的实例并正常运行。如果互斥锁存在,则需要将命令行参数传递给已经通过P2P队列运行的那个,然后退出。 检查互斥锁后,第一个实例生成一个工作线程。该线程在P2P队列上侦听消息。当他们进来时,你会处理它们。     

要回复问题请先登录注册