Giter Site home page Giter Site logo

u-c-s / singleinstancecore Goto Github PK

View Code? Open in Web Editor NEW

This project forked from soheilkd/singleinstancecore

1.0 1.0 0.0 39 KB

For developing single instance application - For Windows applications on .NET Core/ .NET 5

License: MIT License

C# 100.00%

singleinstancecore's Introduction

SingleInstanceCore

For single instance applications on .NET Core

NuGet Package: https://www.nuget.org/packages/SingleInstanceCore/

Usage

Note: 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):

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):

	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.

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
	}
}

singleinstancecore's People

Contributors

nikeee avatar soheilkd avatar u-c-s avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.