仅启动一次应用程序(单声道)
我目前正在用c#开发一个mono应用程序,我只想开始一次.我知道,这可以通过互斥锁来实现.但是,如何使用Mono将应用程序置于最前面呢? 我尝试通过
I'm currently developing an mono application in c#, which I would like to start only once. I know, this can be achieved with mutex. But how can I bring the application to front using mono? I tried getting the Process via
Process.GetProcessByName("AudioCuesheetEditor")
但无法访问MainWindowHandle.
but couldn't access the MainWindowHandle.
如何将正在运行的应用程序置于最前面?
How can I bring the running application to front?
感谢您的回答.
现在我已经能够获取MainWindowHandle,但这是一个IntPtr.我该如何把这个把手放在前面?我尝试过
Now I have been able to get the MainWindowHandle, but it's a IntPtr. How do I bring this handle to front? I tried
Window wRunning = new Window(handle);
wRunning.Present();
但这给了我一个例外:(.
but that gave me an exception :(.
我已经可以使用文件系统监视程序对其进行修复:
I have been able to fix it with a file system watcher:
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
}