- using System;
- using System.Drawing;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting.Channels.Tcp;
- using System.Threading;
- namespace MyWinApp
- {
- /// <summary>
- /// Summary description for MainForm.
- /// </summary>
- public class MainForm : System.Windows.Forms.Form
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.Container components = null;
- // Static instance of the form
- static MainForm m_mainForm = new MainForm();
- // Signature of method to call when another instance of the form is detected
- delegate void OtherInstanceCallback(string[] args);
- public MainForm()
- {
- // Required for Windows Form Designer support
- InitializeComponent();
- }
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- // MainForm
- this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
- this.ClientSize = new System.Drawing.Size(292, 266);
- this.Name = "MainForm";
- this.Text = "MainForm";
- }
- #endregion
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main(string[] args)
- {
- /*
- * Check to see if this is the first instance of the application. If it is, then just
- * run the app. If it's not, then use remoting and a callback to communicate with the
- * other instance.
- *
- * The safeName variable is used as the name of the mutex, and by using the UserAppDataPath
- * we can be sure the name is unique. Also, backslashes are not allowed in mutexes, so
- * we must replace them with something else, in this case an underscore.
- */
- const int REMOTING_PORT = 1234;
- const string REMOTING_CHANNEL = "MyWinApp";
- bool firstInstance = false;
- string safeName = Application.UserAppDataPath.Replace(@"\\", "_");
- Mutex mtx = new Mutex(true, safeName, out firstInstance);
- if (!firstInstance)
- {
- // Open remoting channel exposed from initial instance
- string url = string.Format("<a href="tcp://localhost:">tcp://localhost:{0}/{1}", REMOTING_PORT, REMOTING_CHANNEL);
- MainForm otherMainForm = (MainForm)RemotingServices.Connect(typeof(MainForm), url);
- // Send arguments to initial instance and exit this one
- otherMainForm.OnOtherInstance(args);
- return;
- }
- // Expose remoting channel to accept arguments from other instances
- ChannelServices.RegisterChannel(new TcpChannel(REMOTING_PORT));
- RemotingServices.Marshal(m_mainForm, REMOTING_CHANNEL);
- // Finally run it
- Application.Run(new MainForm());
- // Not 100% sure this needs to be called, but doing it just in case. Testing has not
- // shown any negative side effects.
- mtx.ReleaseMutex();
- }
- // Used to help prevent mu
- //该片段来自于http://www.codesnippet.cn/detail/201220121090.html
来源: http://www.codesnippet.cn/detail/201220121090.html