Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/soheilkd/singleinstancecore
For developing single instance application - For Windows applications on .NET Core/ .NET 5
https://github.com/soheilkd/singleinstancecore
single-instance single-instance-app windows windows-desktop windows-forms wpf
Last synced: 23 days ago
JSON representation
For developing single instance application - For Windows applications on .NET Core/ .NET 5
- Host: GitHub
- URL: https://github.com/soheilkd/singleinstancecore
- Owner: soheilkd
- License: mit
- Created: 2020-03-09T14:41:09.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-04-30T19:50:08.000Z (over 2 years ago)
- Last Synced: 2024-10-01T00:01:36.641Z (about 1 month ago)
- Topics: single-instance, single-instance-app, windows, windows-desktop, windows-forms, wpf
- Language: C#
- Homepage:
- Size: 37.1 KB
- Stars: 34
- Watchers: 5
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SingleInstanceCore
For single instance applications on .NET CoreNuGet Package: https://www.nuget.org/packages/SingleInstanceCore/
# UsageNote: Usage examples are for WPF and Winforms desktop applications. For other platforms/frameworks, inheritance and initialization should be done accordingly, not exactly like the examples.
The class that handles instance invokation should inherit ISingleInstance and implement OnInstanceInvoked method.
## WPF
E.g. in App class (`App.xaml.cs`):
```csharp
public partial class App : Application, ISingleInstance
{
public void OnInstanceInvoked(string[] args)
{
// What to do with the args another instance has sent
}
// ...
}
```
Initialization of instance should be done when application is starting, and cleanup method should be called on the exit point of the application.E.g. in App class (`App.xaml.cs`):
```csharp
private void Application_Startup(object sender, StartupEventArgs e)
{
bool isFirstInstance = this.InitializeAsFirstInstance("soheilkd_ExampleIPC");
if (!isFirstInstance)
{
//If it's not the first instance, arguments are automatically passed to the first instance
//OnInstanceInvoked will be raised on the first instance
//You may shut down the current instance
Current.Shutdown();
}
}
private void Application_Exit(object sender, ExitEventArgs e)
{
//Do not forget to cleanup
SingleInstance.Cleanup();
}
```## Winforms
Winforms doesn't have an Application.cs that could implement the `ISingleInstance` interface. We have to define one ourselves.
```csharp
static class Program
{
[STAThread]
static void Main(string[] args)
{
var app = new YourApplication();var isFirstInstance = app.InitializeAsFirstInstance(nameof(YourApplication));
if (isFirstInstance)
{
try
{
app.Run();
}
finally
{
SingleInstance.Cleanup();
}
}
}
}class YourApplication : ISingleInstance
{
public void Run()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm()); // Blocks until the main window is closed
}public void OnInstanceInvoked(string[] args)
{
// What to do with the args another instance has sent
}
}```