所以我写了一个完美的C#(WindowsForm)应用程序。但我希望能够在命令行中使用它并添加参数。如何跨进程使用相同的变量?
我想要做的就是说“9 4 3 5”的pass命令行参数执行一些代码并用数据填充变量,然后通过命令“run trial”并从试验代码中提取试验代码填入“9 4 3 5”命令行。
我有一切标记为静态,但它仍然无法正常工作。我的假设是因为一个新的.exe进程正在从命令行调用,并启动一个新的进程与新的变量值等
我该如何保留旧的进程,或让变量持有所有进程的值?
是我唯一的选择像内存映射文件吗?我想我必须将数据写入磁盘,然后每次都读取它。
我还需要更改NotifyIcon
图标以及通过参数从命令行,所以我不认为写入磁盘将有所帮助。可以将工作负载放入DLL帮助中,还是放入其自己的单独类中,然后从每个实例访问它?
谢谢大家!
编辑:添加信息
感谢lehiester你为我开始。
我现在有这样的代码:
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();
}
}
}
我现在的问题是,它仍然显示第二NotifyIcon
并删除它在鼠标悬停在..确切位置在哪里我应该打电话mBase.Run(Environment.GetCommandLineArgs());
?
[问]和[MCVE]也[你是什么意思它仍然没有工作(https://开头元.stackexchange.com/questions/147616/what-do-you-mean-it-doesnt-work)? – john