Search code examples
c#windows-mobilemessageinterceptorsingle-instance

How to pass arguments to a console application if it is already running?


I use a Console Application in Windows Mobile to handle incoming message interception. In the same console application i accept parameters (string args[]) which based on the parameters, register the message interceptor.

InterceptorType is a 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



    }

I made this a console application because i want it keep running in the background and intercept incoming messages.

This works fine for the first time. But the problem is that I have to keep calling the addInterception method to add subsequent interception rules. This makes the console application start again and again for each time i add a rule. How do i make this run only once and add more message interceptor rules?


Solution

  • Since you already have a method in place to call the command prompt once, update your logic with some simple looping so you can pass N commands.

    EDIT: I wrote it a fully compileable example to show you exactly what I am talking about. Note how the child process can be called any number of times without re-launching. This is not just a simple command line launch with arguments being passed because that idea will lead to X processes which is exactly what you do not want.

    PARENT PROCESS: (The one with 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();
            }
    
        }
    

    CHILD PROCESS:

        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  
            }  
        }
    }
    

    Note that codeplex has a managed service library.