{"id":14986397,"url":"https://github.com/soheilkd/singleinstancecore","last_synced_at":"2025-04-11T20:32:28.411Z","repository":{"id":45832028,"uuid":"246060919","full_name":"soheilkd/SingleInstanceCore","owner":"soheilkd","description":"For developing single instance application - For Windows applications on .NET Core/ .NET 5","archived":false,"fork":false,"pushed_at":"2022-04-30T19:50:08.000Z","size":38,"stargazers_count":35,"open_issues_count":1,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-22T02:48:04.862Z","etag":null,"topics":["single-instance","single-instance-app","windows","windows-desktop","windows-forms","wpf"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soheilkd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-09T14:41:09.000Z","updated_at":"2025-02-15T02:42:50.000Z","dependencies_parsed_at":"2022-09-24T01:40:24.610Z","dependency_job_id":null,"html_url":"https://github.com/soheilkd/SingleInstanceCore","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soheilkd%2FSingleInstanceCore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soheilkd%2FSingleInstanceCore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soheilkd%2FSingleInstanceCore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soheilkd%2FSingleInstanceCore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soheilkd","download_url":"https://codeload.github.com/soheilkd/SingleInstanceCore/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248476515,"owners_count":21110300,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["single-instance","single-instance-app","windows","windows-desktop","windows-forms","wpf"],"created_at":"2024-09-24T14:12:48.726Z","updated_at":"2025-04-11T20:32:26.675Z","avatar_url":"https://github.com/soheilkd.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SingleInstanceCore\nFor single instance applications on .NET Core\n\nNuGet Package: https://www.nuget.org/packages/SingleInstanceCore/\n# Usage\n\nNote: 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.\n\nThe class that handles instance invokation should inherit ISingleInstance and implement OnInstanceInvoked method.\n\n## WPF\nE.g. in App class (`App.xaml.cs`):\n```csharp\npublic partial class App : Application, ISingleInstance\n{\n\tpublic void OnInstanceInvoked(string[] args)\n\t{\n\t\t// What to do with the args another instance has sent\n\t}\n\t// ...\n}\n```\nInitialization of instance should be done when application is starting, and cleanup method should be called on the exit point of the application. \n\nE.g. in App class (`App.xaml.cs`):\n```csharp\n\tprivate void Application_Startup(object sender, StartupEventArgs e)\n\t{\n\t\tbool isFirstInstance = this.InitializeAsFirstInstance(\"soheilkd_ExampleIPC\");\n\t\tif (!isFirstInstance)\n\t\t{\n\t\t\t//If it's not the first instance, arguments are automatically passed to the first instance\n\t\t\t//OnInstanceInvoked will be raised on the first instance\n\t\t\t//You may shut down the current instance\n\t\t\tCurrent.Shutdown();\n\t\t}\n\t}\n\t\t\n\tprivate void Application_Exit(object sender, ExitEventArgs e)\n\t{\n\t\t//Do not forget to cleanup\n\t\tSingleInstance.Cleanup();\n\t}\n```\n\n## Winforms\nWinforms doesn't have an Application.cs that could implement the `ISingleInstance` interface. We have to define one ourselves.\n```csharp\nstatic class Program\n{\n\t[STAThread]\n\tstatic void Main(string[] args)\n\t{\n\t\tvar app = new YourApplication();\n\n\t\tvar isFirstInstance = app.InitializeAsFirstInstance(nameof(YourApplication));\n\t\tif (isFirstInstance)\n\t\t{\n\t\t\ttry\n\t\t\t{\n\t\t\t\tapp.Run();\n\t\t\t}\n\t\t\tfinally\n\t\t\t{\n\t\t\t\tSingleInstance.Cleanup();\n\t\t\t}\n\t\t}\n\t}\n}\n\nclass YourApplication : ISingleInstance\n{\n\tpublic void Run()\n\t{\n\t\tApplication.SetHighDpiMode(HighDpiMode.SystemAware);\n\t\tApplication.EnableVisualStyles();\n\t\tApplication.SetCompatibleTextRenderingDefault(false);\n\t\tApplication.Run(new MainForm()); // Blocks until the main window is closed\n\t}\n\n\tpublic void OnInstanceInvoked(string[] args)\n\t{\n\t\t// What to do with the args another instance has sent\n\t}\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoheilkd%2Fsingleinstancecore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoheilkd%2Fsingleinstancecore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoheilkd%2Fsingleinstancecore/lists"}