1

So I wrote a C# (WindowsForm) app that works perfectly. But I want to be able to use it from the command line with args added.

What I am aiming to do is to say pass command line args of "9 4 3 5" execute some code and fill variables with data, then pass command "run trial" and have the trial code pull from the variables that the "9 4 3 5" command line filled.

I have everything marked as static, but its still not working. My assumption is because a new .exe process is being called from the command line and starting a new process with new variable values etc.

How can I either keep the old process, or have the variable hold the value for all processes?

Is something like a memory mapped file my only option? I think I have to write data to the disk and then fetch it each time.

I also need to change the NotifyIcon icon as well from command line via arguments, so I don't think writing to the disk will help. Could putting the workload into a DLL help, or into its own separate class then access that from each instance?

Thanks everyone!

EDIT: ADDED INFO

Thanks to lehiester for getting me started.

I now have this code:

class AppBase : WindowsFormsApplicationBase
    {
        internal AppBase() : base()
        {
            this.IsSingleInstance = true;
            this.MainForm = new TrayIcon();
        }
    }

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new TrayIcon());
            AppBase mBase = new AppBase();
            mBase.StartupNextInstance += MBase_StartupNextInstance;
            mBase.Run(Environment.GetCommandLineArgs());
        }

        private static void MBase_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
        {
            if(e.CommandLine[1].Equals("toggle"))
            {
                TrayIcon.toggleIcon();
            }
        }
    }

My current issue is that it still shows a second NotifyIcon and removes it upon mouse hover over.. Where exactly should I be calling mBase.Run(Environment.GetCommandLineArgs());?

Seth
  • 1,769
  • 4
  • 26
  • 39
  • 1
    [ask] and [mcve] also [what do you mean it's still not working](https://meta.stackexchange.com/questions/147616/what-do-you-mean-it-doesnt-work) ? – ProgrammingLlama Oct 10 '17 at 02:34

2 Answers2

4

Your assumption is correct--the static variables aren't working because they are owned by the process (actually the AppDomain, strictly speaking), and new instances are being started in separate processes. Multiple .NET programs cannot share a single static variable except for very special cases where they are hosted in the same process, which is probably not possible in this case.

In general, the easiest way to pass data between processes is usually named pipes, but Windows Forms has special support for passing the command line arguments for a second instance to the already running instance. You can use the WindowsFormsApplicationBase.StartupNextInstance event, as explained in this post.

lehiester
  • 836
  • 2
  • 7
  • 17
  • This is what I am looking for.. added more details to my question, thanks for getting me on the right track @lehiester – Seth Oct 10 '17 at 03:14
  • I got it with the `Startup` method of` WindowsFormsApplicationBase` – Seth Oct 10 '17 at 04:36
1

I certainly would recommend keeping it simple. Consider emitting the calculated results to standard out and then piping that to the second argument, something like:

first-process 9 4 3 5 | second-process  
Randy Larson
  • 6,817
  • 2
  • 18
  • 13