Initial
This commit is contained in:
parent
806d17f954
commit
bb1282e066
25
Mk0.Tools.SingleInstance.sln
Normal file
25
Mk0.Tools.SingleInstance.sln
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio 15
|
||||||
|
VisualStudioVersion = 15.0.28307.438
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mk0.Tools.SingleInstance", "Mk0.Tools.SingleInstance\Mk0.Tools.SingleInstance.csproj", "{EE314A04-04C0-4FFB-999C-893F61EC59BD}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{EE314A04-04C0-4FFB-999C-893F61EC59BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{EE314A04-04C0-4FFB-999C-893F61EC59BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{EE314A04-04C0-4FFB-999C-893F61EC59BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{EE314A04-04C0-4FFB-999C-893F61EC59BD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {ACE43D6F-6DD1-4695-9A29-F0316CDFE039}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
43
Mk0.Tools.SingleInstance/Mk0.Tools.SingleInstance.csproj
Normal file
43
Mk0.Tools.SingleInstance/Mk0.Tools.SingleInstance.csproj
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProjectGuid>{EE314A04-04C0-4FFB-999C-893F61EC59BD}</ProjectGuid>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>Mk0.Tools.SingleInstance</RootNamespace>
|
||||||
|
<AssemblyName>Mk0.Tools.SingleInstance</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<Deterministic>true</Deterministic>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Windows.Forms" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="SingleApplication.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
</Project>
|
36
Mk0.Tools.SingleInstance/Properties/AssemblyInfo.cs
Normal file
36
Mk0.Tools.SingleInstance/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// Allgemeine Informationen über eine Assembly werden über die folgenden
|
||||||
|
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
|
||||||
|
// die einer Assembly zugeordnet sind.
|
||||||
|
[assembly: AssemblyTitle("Mk0.Tools.SingleInstance")]
|
||||||
|
[assembly: AssemblyDescription("")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("")]
|
||||||
|
[assembly: AssemblyProduct("Mk0.Tools.SingleInstance")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright © 2019 mk0.at")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
|
||||||
|
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
|
||||||
|
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
|
||||||
|
[assembly: Guid("ee314a04-04c0-4ffb-999c-893f61ec59bd")]
|
||||||
|
|
||||||
|
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
|
||||||
|
//
|
||||||
|
// Hauptversion
|
||||||
|
// Nebenversion
|
||||||
|
// Buildnummer
|
||||||
|
// Revision
|
||||||
|
//
|
||||||
|
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
|
||||||
|
// indem Sie "*" wie unten gezeigt eingeben:
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
[assembly: AssemblyVersion("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
123
Mk0.Tools.SingleInstance/SingleApplication.cs
Normal file
123
Mk0.Tools.SingleInstance/SingleApplication.cs
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
using System;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Mk0.Tools.SingleInstance
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Summary description for SingleApp.
|
||||||
|
/// </summary>
|
||||||
|
public class SingleApplication
|
||||||
|
{
|
||||||
|
public SingleApplication()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Imports
|
||||||
|
/// </summary>
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
private static extern int SetForegroundWindow(IntPtr hWnd);
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
private static extern int IsIconic(IntPtr hWnd);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// GetCurrentInstanceWindowHandle
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static IntPtr GetCurrentInstanceWindowHandle()
|
||||||
|
{
|
||||||
|
IntPtr hWnd = IntPtr.Zero;
|
||||||
|
Process process = Process.GetCurrentProcess();
|
||||||
|
Process[] processes = Process.GetProcessesByName(process.ProcessName);
|
||||||
|
foreach (Process _process in processes)
|
||||||
|
{
|
||||||
|
if (_process.Id != process.Id &&
|
||||||
|
_process.MainModule.FileName == process.MainModule.FileName &&
|
||||||
|
_process.MainWindowHandle != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
hWnd = _process.MainWindowHandle;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hWnd;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// SwitchToCurrentInstance
|
||||||
|
/// </summary>
|
||||||
|
private static void SwitchToCurrentInstance()
|
||||||
|
{
|
||||||
|
IntPtr hWnd = GetCurrentInstanceWindowHandle();
|
||||||
|
if (hWnd != IntPtr.Zero)
|
||||||
|
{
|
||||||
|
if (IsIconic(hWnd) != 0)
|
||||||
|
{
|
||||||
|
ShowWindow(hWnd, SW_RESTORE);
|
||||||
|
}
|
||||||
|
|
||||||
|
SetForegroundWindow(hWnd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Execute a form base application if another instance already running on
|
||||||
|
/// the system activate previous one
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="frmMain">main form</param>
|
||||||
|
/// <param name="appSetting">your app can set if single instance (true) or multi instance (false)</param>
|
||||||
|
/// <returns>true if no previous instance is running</returns>
|
||||||
|
public static bool Run(Form frmMain, bool appSetting=true)
|
||||||
|
{
|
||||||
|
if (IsAlreadyRunning() && appSetting)
|
||||||
|
{
|
||||||
|
SwitchToCurrentInstance();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Application.Run(frmMain);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// for console base application
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool Run()
|
||||||
|
{
|
||||||
|
if (IsAlreadyRunning())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// check if given exe alread running or not
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>returns true if already running</returns>
|
||||||
|
private static bool IsAlreadyRunning()
|
||||||
|
{
|
||||||
|
string strLoc = Assembly.GetExecutingAssembly().Location;
|
||||||
|
FileSystemInfo fileInfo = new FileInfo(strLoc);
|
||||||
|
string sExeName = fileInfo.Name;
|
||||||
|
|
||||||
|
mutex = new Mutex(true, "Global\\" + sExeName, out bool bCreatedNew);
|
||||||
|
if (bCreatedNew)
|
||||||
|
{
|
||||||
|
mutex.ReleaseMutex();
|
||||||
|
}
|
||||||
|
|
||||||
|
return !bCreatedNew;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Mutex mutex;
|
||||||
|
const int SW_RESTORE = 9;
|
||||||
|
}
|
||||||
|
}
|
15
README.md
15
README.md
@ -1 +1,16 @@
|
|||||||
# Mk0.Tools.SingleInstance
|
# Mk0.Tools.SingleInstance
|
||||||
|
(C) 2019 mk0.at
|
||||||
|
|
||||||
|
With this code you are able to make your application a single instance.
|
||||||
|
|
||||||
|
Usage example:
|
||||||
|
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Application.EnableVisualStyles();
|
||||||
|
Application.SetCompatibleTextRenderingDefault(false);
|
||||||
|
SingleApplication.Run(new MainForm(), Demo.Properties.Settings.Default.singleInstance);
|
||||||
|
}
|
||||||
|
|
||||||
|
MainForm is the form to show on start
|
||||||
|
singleInstance is an optional boolean value, where you can enable/disable SingleInstance within your application settings
|
Loading…
Reference in New Issue
Block a user