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());
?