仅启动应用程序一次(单声道)

时间:2013-07-25 10:18:55

标签: c# process mono mutex

我目前正在开发c#中的单声道应用程序,我想只启动一次。我知道,这可以用互斥量来实现。但是如何使用mono将应用程序带到前面呢? 我尝试通过

获取流程
Process.GetProcessByName("AudioCuesheetEditor")

但无法访问MainWindowHandle。

如何将正在运行的应用程序带到前面?

谢谢你的答案。

编辑:

现在我已经能够获得MainWindowHandle,但它是一个IntPtr。如何将此手柄放在前面?我试过了

Window wRunning = new Window(handle);
wRunning.Present();

但这给了我一个例外:(。

2 个答案:

答案 0 :(得分:0)

我已经能够使用文件系统观察器修复它:

        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
        }

答案 1 :(得分:0)

使用FSW看起来非常h​​acky。

我认为最优雅的解决方案是使用IPC。

特别是,我会使用DBus。事实上,Banshee用来避免创建多个实例。如果您有兴趣,请继续查看我们的源代码。