예제 #1
0
        public void TestMutex_Finalizer()
        {
            var mutexId       = Guid.NewGuid().ToString();
            var resourceMutex = ResourceMutex.Create(mutexId, "TestMutex_Finalizer");

            Assert.NotNull(resourceMutex);
            Assert.True(resourceMutex.IsLocked);
        }
예제 #2
0
        public void TestMutex_Create_Cleanup()
        {
            var mutexId = Guid.NewGuid().ToString();

            using (var resourceMutex = ResourceMutex.Create(mutexId, "TestMutex_Create_Cleanup"))
            {
                Assert.NotNull(resourceMutex);
                Assert.True(resourceMutex.IsLocked);
            }
        }
예제 #3
0
        public Task StartAsync(CancellationToken cancellationToken)
        {
            _resourceMutex = ResourceMutex.Create(null, _mutexConfig.MutexId, _hostingEnvironment.ApplicationName, _mutexConfig.IsGlobal);

            _appLifetime.ApplicationStopping.Register(OnStopping);
            if (!_resourceMutex.IsLocked)
            {
                _mutexConfig.WhenNotFirstInstance?.Invoke(_hostingEnvironment);
                _logger.LogDebug("Application {0} already running, stopping application.", _hostingEnvironment.ApplicationName);
                _appLifetime.StopApplication();
            }

            return(Task.CompletedTask);
        }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            this.resourceMutex = ResourceMutex.Create(null, this.mutexBuilder.MutexId, this.hostEnvironment.ApplicationName, this.mutexBuilder.IsGlobal);

            this.hostApplicationLifetime.ApplicationStopping.Register(OnStopping);
            if (!this.resourceMutex.IsLocked)
            {
                this.mutexBuilder.WhenNotFirstInstance?.Invoke(this.hostEnvironment, this.logger);
                this.logger.LogDebug("Application {0} already running, stopping application.", this.hostEnvironment.ApplicationName);
                this.hostApplicationLifetime.StopApplication();
            }

            return(Task.CompletedTask);
        }
예제 #5
0
        public void TestMutex_100()
        {
            var mutexId = Guid.NewGuid().ToString();
            // Test creating and cleanup 100x
            var i = 0;

            do
            {
                using (var resourceMutex = ResourceMutex.Create(mutexId, "Call" + i))
                {
                    Assert.NotNull(resourceMutex);
                    Assert.True(resourceMutex.IsLocked);
                }
            } while (i++ < 100);
        }
예제 #6
0
        public async Task TestMutex_LockTwice()
        {
            var mutexId = Guid.NewGuid().ToString();

            using (var resourceMutex = ResourceMutex.Create(mutexId, "FirstCall"))
            {
                Assert.NotNull(resourceMutex);
                Assert.True(resourceMutex.IsLocked);
                await Task.Factory.StartNew(() =>
                {
                    using (var resourceMutex2 = ResourceMutex.Create(mutexId, "SecondCall"))
                    {
                        Assert.NotNull(resourceMutex2);
                        Assert.False(resourceMutex2.IsLocked);
                    }
                }, default, TaskCreationOptions.LongRunning, TaskScheduler.Default);