RequestSingleInstanceLockAsync callback does not seem to work #610

Closed
opened 2026-01-29 16:44:03 +00:00 by claunia · 3 comments
Owner

Originally created by @Delpire on GitHub (Dec 8, 2020).

Originally assigned to: @GregorBiswanger on GitHub.

  • Version: 9.31.2
  • Target: Windows 10

I cannot get the RequestSingleInstanceLockAsync callback to work at all. I have tried several things and it just does not seem to get called.

I have set up a test to see if its firing. Basically, if a second instance is launched, I just want it to kill the first instance. It does not kill the app. In my actual app, I want it to restore the window if necessary and focus the window; that wouldn't work either. It seems the callback just isn't being called at all.

The lock is working, it won't launch the second instance. (Although it does launch the splash screen which is not very nice).

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    app.UseRequestLocalization(localizationOptions);

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapBlazorHub();
        endpoints.MapFallbackToPage("/_Host");
    });

    if (HybridSupport.IsElectronActive)
    {
        var task = Electron.App.RequestSingleInstanceLockAsync(MyCallback);
        var hasLock = task.GetAwaiter().GetResult();
        if (!hasLock)
        {
            Electron.App.Exit();
            return;
        }
        // Normal browser window setup, etc.
    }
}

public void MyCallback(string[] args, string directory)
{
    Electron.App.Exit(); // !!! This is never executed.
}
        

Any idea what is going on here? Or am I using this incorrectly?

Originally created by @Delpire on GitHub (Dec 8, 2020). Originally assigned to: @GregorBiswanger on GitHub. <!-- Please search existing issues to avoid creating duplicates. --> <!-- Which version of Electron.NET CLI and API are you using? --> <!-- Please always try to use latest version before report. --> * **Version**: 9.31.2 <!-- Which version of .NET Core and Node.js are you using (if applicable)? --> <!-- What target are you building for? --> * **Target**: Windows 10 <!-- Enter your issue details below this comment. --> <!-- If you want, you can donate to increase issue priority (https://donorbox.org/electron-net) --> I cannot get the RequestSingleInstanceLockAsync callback to work at all. I have tried several things and it just does not seem to get called. I have set up a test to see if its firing. Basically, if a second instance is launched, I just want it to kill the first instance. It does not kill the app. In my actual app, I want it to restore the window if necessary and focus the window; that wouldn't work either. It seems the callback just isn't being called at all. The lock is working, it won't launch the second instance. (Although it does launch the splash screen which is not very nice). ```csharp // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseRequestLocalization(localizationOptions); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapBlazorHub(); endpoints.MapFallbackToPage("/_Host"); }); if (HybridSupport.IsElectronActive) { var task = Electron.App.RequestSingleInstanceLockAsync(MyCallback); var hasLock = task.GetAwaiter().GetResult(); if (!hasLock) { Electron.App.Exit(); return; } // Normal browser window setup, etc. } } public void MyCallback(string[] args, string directory) { Electron.App.Exit(); // !!! This is never executed. } ``` Any idea what is going on here? Or am I using this incorrectly?
claunia added the bug label 2026-01-29 16:44:03 +00:00
Author
Owner

@matanelrb commented on GitHub (Feb 12, 2021):

Hi, any idea when will this bug get fixed?

@matanelrb commented on GitHub (Feb 12, 2021): Hi, any idea when will this bug get fixed?
Author
Owner

@matanelrb commented on GitHub (Feb 15, 2021):

I have worked out a temporary fix. Since I can tell apart what are the first and second instance, I can use named pipes in order to send over the arguments to the first instance. And on the first instance have a thread which will run at the background which will listen to the named pipe and act accordingly. Problem is I can't extract the arguments out of the second instance since they don't have switches. So is there any way of extracting the whole arguments array? I need the single instance use in order to implement OAuth for my app so there is only one argument which is passed to the second instance.
Achieving that will give me a full temporary solution to the problem until you fix the bug.

Thanks.

@matanelrb commented on GitHub (Feb 15, 2021): I have worked out a temporary fix. Since I can tell apart what are the first and second instance, I can use named pipes in order to send over the arguments to the first instance. And on the first instance have a thread which will run at the background which will listen to the named pipe and act accordingly. Problem is I can't extract the arguments out of the second instance since they don't have switches. So is there any way of extracting the whole arguments array? I need the single instance use in order to implement OAuth for my app so there is only one argument which is passed to the second instance. Achieving that will give me a full temporary solution to the problem until you fix the bug. Thanks.
Author
Owner

@GregorBiswanger commented on GitHub (Jul 2, 2021):

Hi @matanelrb
the support of handle command line arguments in singleInstance is implemented for the next Electron.NET 13.5.1 release.

see https://github.com/ElectronNET/Electron.NET/issues/520

@GregorBiswanger commented on GitHub (Jul 2, 2021): Hi @matanelrb the support of handle command line arguments in singleInstance is implemented for the next Electron.NET 13.5.1 release. see https://github.com/ElectronNET/Electron.NET/issues/520
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#610