【发布时间】:2018-11-26 08:12:22
【问题描述】:
我有一个 winform 应用程序。用于阻止创建多个实例的 Mutex 类。
static class Program
{
public static string IDFromAnotherApp = "default";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
IDFromAnotherApp = args[0];
bool successAquisition;
Mutex programMutex = new Mutex(true,
AppDomain.CurrentDomain.SetupInformation.ApplicationName,
out successAquisition);
if (successAquisition)
{
Application.Run(new Form1());
programMutex.ReleaseMutex();
GC.KeepAlive(programMutex);
}
else
{
IDFromAnotherApp = "another";
MessageBox.Show("already open");
}
}
}
Form1 代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DisplayId();
}
public void DisplayId()
{
label1.Text = Program.IDFromAnotherApp;
}
}
现在当用户点击带参数的APP时,Form1 label1控件将呈现该值,但如果另一个用户再次点击APP,弹出窗口会说“已经打开”
我想做得更好,不是弹出窗口,当另一个用户点击带有参数的APP时,已经打开的Form1可以更新它的label1的文本吗?这甚至可能吗?
【问题讨论】: