From: <che...@us...> - 2009-10-24 20:17:10
|
Revision: 3134 http://mp-plugins.svn.sourceforge.net/mp-plugins/?rev=3134&view=rev Author: chef_koch Date: 2009-10-24 20:17:01 +0000 (Sat, 24 Oct 2009) Log Message: ----------- prevent IRSSConfig running multiple times feel free to modify ProcessHelper, if needed. can also be used for other apps, but those may designed to be run to listen to different ir servers on network Modified Paths: -------------- trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssUtils/IrssUtils.csproj trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Program.cs Added Paths: ----------- trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssUtils/ProcessHelper.cs Modified: trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssUtils/IrssUtils.csproj =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssUtils/IrssUtils.csproj 2009-10-24 18:17:01 UTC (rev 3133) +++ trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssUtils/IrssUtils.csproj 2009-10-24 20:17:01 UTC (rev 3134) @@ -2,7 +2,7 @@ <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> - <ProductVersion>9.0.30729</ProductVersion> + <ProductVersion>9.0.21022</ProductVersion> <SchemaVersion>2.0</SchemaVersion> <ProjectGuid>{CA15769C-232E-4CA7-94FD-206A06CA3ABB}</ProjectGuid> <OutputType>Library</OutputType> @@ -246,6 +246,7 @@ <DesignTime>True</DesignTime> <DependentUpon>Resources.resx</DependentUpon> </Compile> + <Compile Include="ProcessHelper.cs" /> <Compile Include="SystemRegistry.cs" /> <Compile Include="IRServerInfo.cs" /> <Compile Include="VariableList.cs" /> Added: trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssUtils/ProcessHelper.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssUtils/ProcessHelper.cs (rev 0) +++ trunk/plugins/IR Server Suite/IR Server Suite/Common/IrssUtils/ProcessHelper.cs 2009-10-24 20:17:01 UTC (rev 3134) @@ -0,0 +1,68 @@ +using System; +using System.Diagnostics; +using System.Reflection; +using System.Runtime.InteropServices; + +namespace IrssUtils +{ + public static class ProcessHelper + { + /// <summary> + /// Checks if the Process is already running. + /// </summary> + /// <returns>true if process is already running.</returns> + public static bool IsProcessAlreadyRunning() + { + return IsProcessAlreadyRunning(true); + } + + /// <summary> + /// Checks if the Process is already running. + /// </summary> + /// <param name="activateMainForm">If true, the mainForm will be activated, if process is already running.</param> + /// <returns>true if process is already running.</returns> + public static bool IsProcessAlreadyRunning(bool activateMainForm) + { + Process process = RunningInstance(); + if (process == null) return false; + + // activates the main form of the application + if (activateMainForm && process.MainWindowHandle != IntPtr.Zero) + ShowWindow(process.MainWindowHandle, SW_SHOWNORMAL); + + return true; + } + + public static Process RunningInstance() + { + Process current = Process.GetCurrentProcess(); + Process[] processes = Process.GetProcessesByName(current.ProcessName); + + // Loop through the running processes in with the same name + foreach (Process process in processes) + { + // Ignore the current process + if (process.Id == current.Id) continue; + + // Make sure that the process is running from the same exe file. + // todo: is this really needed??? + //if (process.MainModule.FileName == current.MainModule.FileName) + //{ + // Return the other process instance. + return process; + //} + } + + // No other instance was found, return null + return null; + } + + #region InterOp + + private const int SW_SHOWNORMAL = 1; + [DllImport("user32.dll")] + private static extern IntPtr ShowWindow(IntPtr hwnd, int nCmdShow); + + #endregion + } +} Modified: trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Program.cs =================================================================== --- trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Program.cs 2009-10-24 18:17:01 UTC (rev 3133) +++ trunk/plugins/IR Server Suite/IR Server Suite/IR Server/IR Server Configuration/Program.cs 2009-10-24 20:17:01 UTC (rev 3134) @@ -142,6 +142,10 @@ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(true); + // allow only one application instance + if (IrssUtils.ProcessHelper.IsProcessAlreadyRunning()) + return; + IrssLog.LogLevel = IrssLog.Level.Debug; IrssLog.Open("IR Server Configuration.log"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |