Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subsequent instances crash when started together #9

Open
lukaszm opened this issue Oct 19, 2021 · 3 comments
Open

Subsequent instances crash when started together #9

lukaszm opened this issue Oct 19, 2021 · 3 comments

Comments

@lukaszm
Copy link

lukaszm commented Oct 19, 2021

Hi,

I've come across a scenario where subsequent instances crash with the following error:

System.TimeoutException: Gave up waiting for read lock
   at TinyIpc.Synchronization.TinyReadWriteLock.AcquireReadLock() in /_/src/TinyIpc/Synchronization/TinyReadWriteLock.cs:line 130
   at TinyIpc.IO.TinyMemoryMappedFile.Read() in /_/src/TinyIpc/IO/TinyMemoryMappedFile.cs:line 158
   at TinyIpc.Messaging.TinyMessageBus..ctor(ITinyMemoryMappedFile memoryMappedFile, Boolean disposeFile, TimeSpan minMessageAge) in /_/src/TinyIpc/Messaging/TinyMessageBus.cs:line 111
   at TinyIpc.Messaging.TinyMessageBus..ctor(ITinyMemoryMappedFile memoryMappedFile, Boolean disposeFile) in /_/src/TinyIpc/Messaging/TinyMessageBus.cs:line 90
   at TinyIpc.Messaging.TinyMessageBus..ctor(String name) in /_/src/TinyIpc/Messaging/TinyMessageBus.cs:line 63
   at SingleInstanceCore.SingleInstance.SignalFirstInstance(String channelName, IList`1 commandLineArgs)
   at SingleInstanceCore.SingleInstance.InitializeAsFirstInstance[T](T instance, String uniqueName)

This happens when multiple files are selected and opened from Windows File Explorer, each file starting an instance of my application and then all these instances end up trying to signal the first instance at the same time. I've worked around this in my code by setting up a named Mutex that only allows one instance at a time calling InitializeAsFirstInstance:

using (var mutex = new Mutex(false, uniqueAppName))
{
    try { mutex.WaitOne(3000); } catch (AbandonedMutexException) { }
                
    try
    {
       if (!this.InitializeAsFirstInstance(uniqueAppName))
           Environment.Exit(0);
    }
    finally
    {
        mutex.ReleaseMutex();
    }
}

This works, but it would probably be better if SingleInstanceCore handled this internally, inside the SingleInstance.SignalFirstInstance method.

Thanks for making this library available!

soheilkd added a commit that referenced this issue Oct 25, 2021
@soheilkd
Copy link
Owner

Try v2.2.1 from nuget.
Let me know if the problem persists.

@lukaszm
Copy link
Author

lukaszm commented Nov 10, 2021

I can still reproduce the problem against version 2.2.1. Same error stack trace, except for the different TinyMessageBus constructor used.

Attaching my test project. I build it in Release mode, then associate some random file extension with SingleInstanceTest.exe, so I can invoke it by opening files from Windows Exporer. I'd then select 15 files at a time and Open them to pretty consistently reproduce the error.

SingleInstanceTest.zip

soheilkd added a commit that referenced this issue Apr 30, 2022
@aadi1295
Copy link

aadi1295 commented Jun 7, 2022

Does it work with .NET 6 WPF?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants