在同一程序的多个实例之间同步

本文关键字:实例 之间 同步 程序 | 更新日期: 2023-09-27 18:31:53

我手上有一个相当复杂的编程问题,所以请耐心等待几分钟。

我决定要在 WPF (C#) 中创建媒体播放器,但我遇到了一些泡菜。

我希望我的应用程序是单个实例,以便当用户双击服务器文件时,程序将只运行一次,并将所有文件排队等待播放。

我尝试了几种方法,包括Microsoft的单实例实现,但似乎没有任何效果,直到我决定创建自己的,就像我一样,实现了它(这可能也在互联网上的某个地方,但它没有出现)

基本上,我使用命名互斥锁来防止打开多个实例,并强制其他实例将其参数写入文件,之后,创建互斥锁的实例将读取该文件。不用说,就性能而言,这是非常非常无效的,但无论如何,这是我对 Main() 函数的实现。请注意,这个 Main() 也是从头开始编写的,因为我不太喜欢 VS2010 自动生成的那个。

static void Main(string[] args)
    {
            string[] arguments = new string[0];
            handler g = new handler();
            bool createdNew = false;
            Mutex lolpaca = new Mutex(true, "lolpacamaximumtrolololololol", out createdNew);
            if (createdNew)
            {
                if (args != null)
                {
                    var MainWindow = new MainWindow();
                    var app = new Application();
                    app.Run(MainWindow);
                    lolpaca.ReleaseMutex();
                    lolpaca.Dispose();
                }
                else
                {
                            Array.Resize(ref arguments, 1);
                            arguments[0] = args[0];
                            string line;
                    //nu mai arunca exceptii nenorocitule
                            while ((line = g.ReadArgs()) != null)
                            {
                                int old_size = arguments.Length;
                                Array.Resize(ref arguments, arguments.Length + 1);
                                arguments[old_size] = line;
                            }

                    var MainWindow = new MainWindow(arguments, arguments.Length);
                    var app = new Application();
                    app.Run(MainWindow);
                    lolpaca.ReleaseMutex();
                    lolpaca.Dispose();
                }
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
            }
            else
            {
                Thread writer = new Thread(new ParameterizedThreadStart(g.WriteArg));
                writer.Start(args);
                writer.Join();
                 try
                {
                    g.WriteArg(args);
                }
                catch (IOException e)
                {
                    MediaPlayerFinal_GUI_new.ExceptionCatcher exp = new MediaPlayerFinal_GUI_new.ExceptionCatcher(e.Source);
                    exp.Show();
                }
            }
    }

我也使用这个类来尝试在线程之间同步

   public class handler
{  
    static string path = @"D:'playlist.txt";
    static FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
    string line;
    string arg;
    bool readerFlag = false;
    public string ReadArgs()
    {
        try
        {
            lock (fs)   // Enter synchronization block
            {
                if (!readerFlag)
                {            // Wait until writer  finishes
                    try
                    {
                        // Waits for the Monitor.Pulse in WriteArg
                        Monitor.Wait(fs);
                    }
                    catch (SynchronizationLockException)
                    {
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }

                TextReader tr = new StreamReader(fs);
                while ((line = tr.ReadLine()) != null)
                {
                    arg = line;
                }
                tr.Close();
                tr.Dispose();
            }
          /*  fs.Close();
            fs.Dispose();*/
            readerFlag = false;
            Monitor.Pulse(fs);
            return arg;
        }
        catch (IOException e)
        {
            MediaPlayerFinal_GUI_new.ExceptionCatcher exp = new MediaPlayerFinal_GUI_new.ExceptionCatcher(e.Source);
            exp.Show();
            return null;
        }
    }
    public void WriteArg(object args)
    {
        lock (fs)
        {
            try
            {
                if (readerFlag)
                {
                    try
                    {
                        Monitor.Wait(fs);   // Wait for the Monitor.Pulse in ReadArgs
                    }
                    catch (SynchronizationLockException)
                    {
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }
                arg = Convert.ToString(args);
                //   FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read);                
                TextWriter tw = new StreamWriter(fs);
                tw.WriteLine(args);
                tw.Close();
                tw.Dispose();

            }
            catch (IOException e)
            {
                MediaPlayerFinal_GUI_new.ExceptionCatcher exp = new MediaPlayerFinal_GUI_new.ExceptionCatcher(e.Source);
                exp.Show();
            }
        }
       /* fs.Close();
        fs.Dispose();*/
        readerFlag = true;
        Monitor.Pulse(fs);
    }

}

现在,基本上,对于每个双击的文件,Windows都会创建Main()函数的一个实例。第一个实例可以控制互斥锁,并继续执行它想做的任何事情。其他实例必须将其参数写入文件。

现在,问题来了:显然,线程(所有线程)都没有正确同步,有时我会收到 IO 异常。我不知道这些异常究竟在哪里抛出,因为 try-catch 块似乎什么也没做。事实上,我相信这比尝试捕获会起作用的要深一些。

那么,如何同步用户双击大量文件时生成的所有线程?此实现适用于最多 3 个双击文件,有时(注意,有时有效,有时无效)适用于 3 个以上的文件(测试最多 9 个)。到目前为止,我在互联网上发现的任何内容都没有说明同一应用程序独立运行的多个实例。

如果你能给我一个例子,那就太好了:)

谢谢。

在同一程序的多个实例之间同步

在同一应用程序的两个实例之间进行通信的最佳方法是使用 IPC。下面请参阅可用于帮助处理单个实例的类示例:

    /// <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事件创建的其他实例的通知。