<commit_before>﻿using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Serialization.Formatters;
using System.Threading;
using System.Windows;
using System.Security;
using System.Runtime.InteropServices;
using System.ComponentModel;

// ========================
// source from http://blogs.microsoft.co.il/blogs/arik/SingleInstance.cs.txt
// modified by cologler.
// alse see: http://codereview.stackexchange.com/questions/20871/single-instance-wpf-application
// ------------
// HOW TO USE
// ------------
// class App : Application, ISingleInstanceApp
// {
//     protected override void OnStartup(StartupEventArgs e)
//     {
//         using (var p = new MicrosoftSingleInstanceProvider<App>(this))
//         {
//             if (p.InitializeAsFirstInstance("{3DD4E9C7-4084-4E46-A0FC-F8B4CD0D55A4}"))
//             {
//                 // current is first instance
//             }
//             else
//             {
//                 this.Shutdown();
//             }
//         }
//     }
//     
//     protected override void OnExit(ExitEventArgs e)
//     {
//         if (p != null)
//         {
//             p.Dispose();
//             p = null;
//         }
//     }
//     

namespace System.Shell
{
    public class MicrosoftSingleInstanceProvider<TApplication> : MicrosoftSingleInstanceProvider
        where TApplication : Application, ISingleInstanceApp
    {
        TApplication App;

        public MicrosoftSingleInstanceProvider(TApplication app)
        {
            this.App = app.ThrowIfNull("app");
        }

        public override void Callback(string[] args)
        {
 	         base.Callback(args);

             this.App.SignalExternalCommandLineArgs(args);
        }
    }

    /// <summary>
    /// This class checks to make sure that only one instance of 
    /// this application is running at a time.
    /// </summary>
    /// <remarks>
    /// Note: this class should be used with some caution, because it does no
    /// security checking. For example, if one instance of an app that uses this class
    /// is running as Administrator, any other instance, even if it is not
    /// running as Administrator, can activate it with command line arguments.
    /// For most apps, this will not be much of an issue.
    /// </remarks>
    public class MicrosoftSingleInstanceProvider : MarshalByRefObject, IDisposable
    {
        #region Private Fields

        /// <summary>
        /// String delimiter used in channel names.
        /// </summary>
        private const string Delimiter = ":";

        /// <summary>
        /// Suffix to the channel name.
        /// </summary>
        private const string ChannelNameSuffix = "SingeInstanceIPCChannel";

        /// <summary>
        /// Remote service name.
        /// </summary>
        private const string RemoteServiceName = "SingleInstanceApplicationService";

        /// <summary>
        /// IPC protocol used (string).
        /// </summary>
        private const string IpcProtocol = "ipc://";

        /// <summary>
        /// Application mutex.
        /// </summary>
        private Mutex singleInstanceMutex;

        /// <summary>
        /// IPC channel for communications.
        /// </summary>
        private IpcServerChannel channel;

        /// <summary>
        /// List of command line arguments for the application.
        /// </summary>
        private string[] commandLineArgs;

        #endregion

        public virtual void Callback(string[] args)
        {

        }

        #region Public Methods

        /// <summary>
        /// Checks if the instance of the application attempting to start is the first instance. 
        /// If not, activates the first instance.
        /// </summary>
        /// <returns>True if this is the first instance of the application.</returns>
        public bool InitializeAsFirstInstance(string uniqueName)
        {
            commandLineArgs = GetCommandLineArgs(uniqueName);

            // Build unique application Id and the IPC channel name.
            var applicationIdentifier = uniqueName + Environment.UserName;

            var channelName = String.Concat(applicationIdentifier, Delimiter, ChannelNameSuffix);

            // Create mutex based on unique application Id to check if this is the first instance of the application. 
            bool firstInstance;
            singleInstanceMutex = new Mutex(true, applicationIdentifier, out firstInstance);
            if (firstInstance)
            {
                CreateRemoteService(channelName);
            }
            else
            {
                SignalFirstInstance(channelName, commandLineArgs);
            }

            return firstInstance;
        }

        /// <summary>
        /// Cleans up single-instance code, clearing shared resources, mutexes, etc.
        /// </summary>
        public void Dispose()<commit_msg>1. easy instance<commit_after>﻿using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Serialization.Formatters;
using System.Threading;
using System.Windows;
using System.Security;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Reflection;

// ========================
// source from http://blogs.microsoft.co.il/blogs/arik/SingleInstance.cs.txt
// modified by cologler.
// alse see: http://codereview.stackexchange.com/questions/20871/single-instance-wpf-application
// ------------
// HOW TO USE
// ------------
// [Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
// class App : Application, ISingleInstanceApp
// {
//     protected override void OnStartup(StartupEventArgs e)
//     {
//         using (var p = new MicrosoftSingleInstanceProvider<App>(this))
//         {
//             if (p.InitializeAsFirstInstance("{3DD4E9C7-4084-4E46-A0FC-F8B4CD0D55A4}"))
//             {
//                 // current is first instance
//             }
//             else
//             {
//                 this.Shutdown();
//             }
//         }
//     }
//     
//     protected override void OnExit(ExitEventArgs e)
//     {
//         if (p != null)
//         {
//             p.Dispose();
//             p = null;
//         }
//     }
//     

namespace System.Shell
{
    public class MicrosoftSingleInstanceProvider<TApplication> : MicrosoftSingleInstanceProvider
        where TApplication : Application, ISingleInstanceApp
    {
        TApplication App;

        public MicrosoftSingleInstanceProvider(TApplication app)
        {
            this.App = app.ThrowIfNull("app");
        }

        public bool InitializeAsFirstInstance()
        {
            var type = typeof (TApplication);
            return base.InitializeAsFirstInstance(type.GUID.ToString());
        }

        public override void Callback(string[] args)
        {
 	         base.Callback(args);

             this.App.SignalExternalCommandLineArgs(args);
        }
    }

    /// <summary>
    /// This class checks to make sure that only one instance of 
    /// this application is running at a time.
    /// </summary>
    /// <remarks>
    /// Note: this class should be used with some caution, because it does no
    /// security checking. For example, if one instance of an app that uses this class
    /// is running as Administrator, any other instance, even if it is not
    /// running as Administrator, can activate it with command line arguments.
    /// For most apps, this will not be much of an issue.
    /// </remarks>
    public class MicrosoftSingleInstanceProvider : MarshalByRefObject, IDisposable
    {
        #region Private Fields

        /// <summary>
        /// String delimiter used in channel names.
        /// </summary>
        private const string Delimiter = ":";

        /// <summary>
        /// Suffix to the channel name.
        /// </summary>
        private const string ChannelNameSuffix = "SingeInstanceIPCChannel";

        /// <summary>
        /// Remote service name.
        /// </summary>
        private const string RemoteServiceName = "SingleInstanceApplicationService";

        /// <summary>
        /// IPC protocol used (string).
        /// </summary>
        private const string IpcProtocol = "ipc://";

        /// <summary>
        /// Application mutex.
        /// </summary>
        private Mutex singleInstanceMutex;

        /// <summary>
        /// IPC channel for communications.
        /// </summary>
        private IpcServerChannel channel;

        /// <summary>
        /// List of command line arguments for the application.
        /// </summary>
        private string[] commandLineArgs;

        #endregion

        public bool IsFirstInstance { get; private set; }

        public virtual void Callback(string[] args)
        {

        }

        #region Public Methods

        /// <summary>
        /// Checks if the instance of the application attempting to start is the first instance. 
        /// If not, activates the first instance.
        /// </summary>
        /// <returns>True if this is the first instance of the application.</returns>
        public bool InitializeAsFirstInstance(string uniqueName)
        {
            commandLineArgs = GetCommandLineArgs(uniqueName);

            // Build unique application Id and the IPC channel name.
            var applicationIdentifier = uniqueName + Environment.UserName;

            var channelName = String.Concat(applicationIdentifier, Delimiter, ChannelNameSuffix);

            // Create mutex based on unique application Id to check if this is the first instance of the application. 
            bool firstInstance;
            singleInstanceMutex = new Mutex(true, applicationIdentifier, out firstInstance);
            if (firstInstance)
            {
                IsFirstInstance = true;
                CreateRemoteService(channelName);
            }
            else
            {
                IsFirstInstance = false;
                SignalFirstInstance(channelName, commandLineArgs);
            }

            return firstInstance;
        }

        /// <summary>
        /// Cleans up single-instance code, clearing shared resources, mutexes, etc.
        /// </summary>
        public void Dispose()
{'fraction_non_alphanumeric': 0.1108886346300534, 'fraction_numerical': 0.004481311975591152, 'mean_word_length': 1.8564814814814814, 'pattern_counts': {'":': 2, '<': 55, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4553743', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2558, 'n_words': 763}