仅启动应用程序一次(Mono)
本文关键字:一次 Mono 启动 应用程序 | 更新日期: 2023-09-27 18:24:57
我目前正在c#中开发一个mono应用程序,我只想启动一次。我知道,这可以通过互斥实现。但是如何使用mono将应用程序推向前台呢?我试着通过获取流程
Process.GetProcessByName("AudioCuesheetEditor")
但无法访问MainWindowHandle。
如何将正在运行的应用程序置于前台?
谢谢你的回答。
编辑:
现在我已经能够获得MainWindowHandle,但它是IntPtr。我怎样把这个把手放在前面?我试过
Window wRunning = new Window(handle);
wRunning.Present();
但这给了我一个例外:(.
我已经能够用一个文件系统观察程序修复它:
FileSystemWatcher fswRunning = new FileSystemWatcher(Path.GetTempPath() + "AudioCuesheetEditor");
fswRunning.Filter = "*.txt";
fswRunning.Changed += delegate(object sender, FileSystemEventArgs e) {
log.debug("FileSystemWatcher called Changed");
if (pAudioCuesheetEditor != null)
{
log.debug("pAudioCuesheetEditor != null");
pAudioCuesheetEditor.getObjMainWindow().Present();
}
};
fswRunning.EnableRaisingEvents = true;
Boolean bAlreadyRunning = false;
Process[] arrPRunning = Process.GetProcesses();
foreach (Process pRunning in arrPRunning)
{
Boolean bCheckProcessMatch = false;
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
if (pRunning.HasExited == false)
{
log.debug("pRunning.ProcessName = " + pRunning.ProcessName + " pRunning.MainWindowTitle = " + pRunning.MainWindowTitle);
if (pRunning.ProcessName.ToLower().Contains("mono"))
{
for (int i = 0; i < pRunning.Modules.Count;i++)
{
if (pRunning.Modules[i].ModuleName.Contains("AudioCuesheetEditor"))
{
bCheckProcessMatch = true;
i = pRunning.Modules.Count;
}
}
}
}
}
else
{
log.debug("pRunning.ProcessName = " + pRunning.ProcessName);
if (pRunning.ProcessName == Process.GetCurrentProcess().ProcessName)
{
bCheckProcessMatch = true;
}
}
log.debug("bCheckProcessMatch == " + bCheckProcessMatch);
if ((pRunning.Id != Process.GetCurrentProcess().Id) && (bCheckProcessMatch == true))
{
log.info("Writing to file " + Path.GetTempPath() + "AudioCuesheetEditor" + Path.DirectorySeparatorChar + "message.txt");
File.WriteAllText(Path.GetTempPath() + "AudioCuesheetEditor" + Path.DirectorySeparatorChar + "message.txt","Present");
bAlreadyRunning = true;
}
}
log.debug("bAlreadyRunning = " + bAlreadyRunning);
if (bAlreadyRunning == false)
{
//Start Application
}
为此使用FSW似乎很麻烦。
我认为最优雅的解决方案是使用IPC。
特别是,我会使用DBus。事实上,这就是Banshee用来避免创建多个实例的方法。因此,如果您感兴趣,请继续查看我们的源代码。