1

だから私は(フォームを使用していない)プログラムを作成したので、フォームではなくクリーンなクラスです。そのため、プログラムの起動時にプロセスをチェックし、別のプロセスが見つかった場合は、それ自体を強制終了します。私はこれを試しました...

static void checkDupe()
{
    string RunningProcess = Process.GetCurrentProcess().ProcessName;
    Process[] processes = Process.GetProcessesByName(RunningProcess);

    if (processes.Length > 1)
    {
        Process.GetCurrentProcess().Kill();
    }
}
4

4 に答える 4

4

Mutexの使用を見てください:

class Program
{
    static Mutex _m;

    static bool IsSingleInstance()
    {
    try
    {
        // Try to open existing mutex.
        Mutex.OpenExisting("PERL");
    }
    catch
    {
        // If exception occurred, there is no such mutex.
        Program._m = new Mutex(true, "PERL");

        // Only one instance.
        return true;
    }
    // More than one instance.
    return false;
    }

    static void Main()
    {
    if (!Program.IsSingleInstance())
    {
        Console.WriteLine("More than one instance"); // Exit program.
    }
    else
    {
        Console.WriteLine("One instance"); // Continue with program.
    }
    // Stay open.
    Console.ReadLine();
    }
}

上記の例は、次のものから取られています。

http://www.dotnetperls.com/mutex

于 2012-11-26T10:04:23.930 に答える
1

同じミューテックスのアイデアですが、より単純なコード:

        bool SingleInstance;
        var mut = new Mutex(true,"UniqueName", out SingleInstance);
        if (SingleInstance) {
           //Run
        } else {
           //Exit
        }

アプリケーションが存在する場合、ミューテックスは破棄されます。解放したり破棄したりする必要はありません。

(私の知る限り)

于 2013-12-23T12:15:36.163 に答える
0

これは私の他の答えからコピーされています:同じプログラムの複数のインスタンス間の同期

以下に、複数のインスタンスのチェックを簡素化するだけでなく、開始パラメーターを2番目のインスタンスから最初のインスタンスに転送できるようにするコードを見つけることができます。

    /// <summary>
        /// Enforces single instance for an application.
        /// </summary>
        public class SingleInstance : IDisposable
        {
            #region Fields

            /// <summary>
            /// The synchronization context.
            /// </summary>
            private readonly SynchronizationContext synchronizationContext;

            /// <summary>
            /// The disposed.
            /// </summary>
            private bool disposed;

            /// <summary>
            /// The identifier.
            /// </summary>
            private Guid identifier = Guid.Empty;

            /// <summary>
            /// The mutex.
            /// </summary>
            private Mutex mutex;

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="SingleInstance"/> class.
            /// </summary>
            /// <param name="identifier">
            /// An identifier unique to this application.
            /// </param>
            /// <param name="args">
            /// The command line arguments.
            /// </param>
            public SingleInstance(Guid identifier, IEnumerable<string> args)
            {
                this.identifier = identifier;

                bool ownsMutex;
                this.mutex = new Mutex(true, identifier.ToString(), out ownsMutex);

                this.synchronizationContext = SynchronizationContext.Current;

                this.FirstInstance = ownsMutex;

                if (this.FirstInstance)
                {
                    this.ListenAsync();
                }
                else
                {
                    this.NotifyFirstInstance(args);
                }
            }

            /// <summary>
            /// Initializes a new instance of the <see cref="SingleInstance"/> class.
            /// </summary>
            /// <param name="identifier">
            /// An identifier unique to this application.
            /// </param>
            public SingleInstance(Guid identifier)
                : this(identifier, null)
            {
            }

            #endregion

            #region Public Events

            /// <summary>
            /// Event raised when arguments are received from successive instances.
            /// </summary>
            public event EventHandler<OtherInstanceCreatedEventArgs> OtherInstanceCreated;

            #endregion

            #region Public Properties

            /// <summary>
            /// Gets a value indicating whether this is the first instance of this application.
            /// </summary>
            public bool FirstInstance { get; private set; }

            #endregion

            #region Implemented Interfaces

            #region IDisposable

            /// <summary>
            /// The dispose.
            /// </summary>
            public void Dispose()
            {
                this.Dispose(true);
                GC.SuppressFinalize(this);
            }

            #endregion

            #endregion

            #region Methods

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">
            /// True if managed resources should be disposed; otherwise, false.
            /// </param>
            protected virtual void Dispose(bool disposing)
            {
                if (this.disposed)
                {
                    return;
                }

                if (disposing)
                {
                    if (this.mutex != null && this.FirstInstance)
                    {
                        this.mutex.WaitOne();
                        this.mutex.ReleaseMutex();
                        this.mutex = null;
                    }
                }

                this.disposed = true;
            }

            /// <summary>
            /// Fires the OtherInstanceCreated event.
            /// </summary>
            /// <param name="arguments">
            /// The arguments to pass with the <see cref="OtherInstanceCreatedEventArgs"/> class.
            /// </param>
            protected virtual void OnOtherInstanceCreated(OtherInstanceCreatedEventArgs arguments)
            {
                EventHandler<OtherInstanceCreatedEventArgs> handler = this.OtherInstanceCreated;

                if (handler != null)
                {
                    handler(this, arguments);
                }
            }

            /// <summary>
            /// Listens for arguments on a named pipe.
            /// </summary>
            private void Listen()
            {
                try
                {
                    using (var server = new NamedPipeServerStream(this.identifier.ToString()))
                    {
                        using (var reader = new StreamReader(server))
                        {
                            server.WaitForConnection();
                            var arguments = new List<string>();

                            while (server.IsConnected)
                            {
                                arguments.Add(reader.ReadLine());
                            }

                            this.synchronizationContext.Post(o => this.OnOtherInstanceCreated(new OtherInstanceCreatedEventArgs(arguments)), null);                        
                        }
                    }

                    // start listening again.
                    this.Listen();
                }
                catch (IOException)
                {
                    // Pipe was broken, listen again.
                    this.Listen();
                }          
            }

            /// <summary>
            /// Listens for arguments being passed from successive instances of the applicaiton.
            /// </summary>
            private void ListenAsync()
            {
                Task.Factory.StartNew(this.Listen, TaskCreationOptions.LongRunning);
            }

            /// <summary>
            /// Passes the given arguments to the first running instance of the application.
            /// </summary>
            /// <param name="arguments">
            /// The arguments to pass.
            /// </param>
            private void NotifyFirstInstance(IEnumerable<string> arguments)
            {
                try
                {
                    using (var client = new NamedPipeClientStream(this.identifier.ToString()))
                    {
                        using (var writer = new StreamWriter(client))
                        {
                            client.Connect(200);

                            if (arguments != null)
                            {
                                foreach (string argument in arguments)
                                {
                                    writer.WriteLine(argument);
                                }
                            }
                        }
                    }
                }
                catch (TimeoutException)
                {
                    // Couldn't connect to server
                }
                catch (IOException)
                {
                    // Pipe was broken
                }
            }



 #endregion
    }

/// <summary>
/// Holds a list of arguments given to an application at startup.
/// </summary>
public class OtherInstanceCreatedEventArgs : EventArgs
{
    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="OtherInstanceCreatedEventArgs"/> class.
    /// </summary>
    /// <param name="args">
    /// The command line arguments.
    /// </param>
    public OtherInstanceCreatedEventArgs(IEnumerable<string> args)
    {
        this.Args = args;
    }

    #endregion

    #region Public Properties

    /// <summary>
    /// Gets the startup arguments.
    /// </summary>
    public IEnumerable<string> Args { get; private set; }

    #endregion
}

次に、メインクラスで、アプリケーションが実行されるまで残るクラスのインスタンスを作成できます。他のインスタンスがプロパティによって作成されているかどうかを確認し、イベントFirstInstanceによって作成された他のインスタンスの通知を受け取ることができます。OtherInstanceCreated

于 2012-11-26T15:25:54.930 に答える
0

実行中のプログラムのインスタンスを名前で返します

private static Process RunningInstance()
{
    var current = Process.GetCurrentProcess();
    var procName = current.ProcessName.Replace(@".vshost", string.Empty);
    var processes = Process.GetProcessesByName(procName);

    return processes.FirstOrDefault(t => t.Id != current.Id && current.SessionId == t.SessionId);
}
于 2012-11-26T10:06:43.013 に答える