Beispiel #1
0
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            instanceLock = new SingleGlobalInstance(TimeSpan.FromSeconds(1), "9b8e7757-9368-4034-9ecd-7892e7f730f7");

            if (!instanceLock.GetMutex())
            {
                MessageBox.Show("System Info is already running");
                Shutdown();
            }

            // Create a default configuration
            var configuration = new ConfigurationBuilder()
                                .SetFileLoadExceptionHandler(x => x.Ignore = true)
                                .AddJsonFile(SettingsHelper.GetSettingsFilePath(), true, true)
                                .Build();

            var serviceProvider = new ServiceCollection()
                                  .AddAssemblyResolver()
                                  .AddRegisteredTypes()
                                  .AddOptions()
                                  .AddSingleton <IConfiguration>(configuration)
                                  .Configure <WindowSettings>(configuration.GetSection(typeof(WindowSettings).Name))
                                  .AddNonGenericLoggerError()
                                  .AddFromAssemblies(new[] { "SystemInfo.Core" })
                                  .AddWindows(Array.Empty <string>())
                                  .BuildServiceProvider();

            notifyIcon             = (TaskbarIcon)FindResource("NotifyIcon");
            notifyIcon.DataContext = serviceProvider.GetRequiredService <INotifyIconViewModel>();
            window = serviceProvider.GetView <MainWindow, IMainWindowViewModel>();
            window.Show();
        }
Beispiel #2
0
    public void OnlyOneInstanceAllowed()
    {
        var gui = Guid.NewGuid().ToString();

        using var instance = new SingleGlobalInstance(gui);
        var hasInstance      = instance.GetMutex();
        var taskNotCancelled = Task.Run(() =>
        {
            using var instance = new SingleGlobalInstance(gui);
            var hasInstance    = instance.GetMutex();
        }).Wait(TimeSpan.FromSeconds(1));

        Assert.IsTrue(hasInstance);
        Assert.IsFalse(taskNotCancelled);
    }
Beispiel #3
0
    public void DisposeReleasesMutex()
    {
        var gui         = Guid.NewGuid().ToString();
        var instance    = new SingleGlobalInstance(gui);
        var hasInstance = instance.GetMutex();

        Assert.IsTrue(hasInstance);
        var task = Task.Run(() =>
        {
            using var instance = new SingleGlobalInstance(gui);
            var hasInstance    = instance.GetMutex();
            Assert.IsTrue(hasInstance);
        });

        instance.Dispose();
        var taskNotCancelled = task.Wait(TimeSpan.FromSeconds(5));

        Assert.IsTrue(taskNotCancelled);
    }
Beispiel #4
0
    public void GetMutexWhenThreadExits()
    {
        var gui = Guid.NewGuid().ToString();

        void Action()
        {
            var instance    = new SingleGlobalInstance(gui);
            var hasInstance = instance.GetMutex();

            Assert.IsTrue(hasInstance);
            Task.Delay(200).Wait();
        }

        var thread1 = new Thread(new ThreadStart(Action));
        var thread2 = new Thread(new ThreadStart(Action));

        thread1.Start();
        thread2.Start();
        var taskNotCancelled1 = thread1.Join(TimeSpan.FromSeconds(1));
        var taskNotCancelled2 = thread2.Join(TimeSpan.FromSeconds(1));

        Assert.IsTrue(taskNotCancelled1);
        Assert.IsTrue(taskNotCancelled2);
    }