让相同的winform程序只启动一个

最近看了一个老外的写的方法,通过进程间通信保证只启动一个程序:
客户端主窗体继承该接口,
SingleInstancing
using System;

namespace SingleInstancing
{
    
/**//// <summary>
    
/// Provides methods which a single instance application can use to in order to be respond to any new instance of it.
    
/// </summary>

    public interface ISingleInstanceEnforcer
    
{
        
/**//// <summary>
        
/// Handles messages received from a new instance of the application.
        
/// </summary>
        
/// <param name="e">The EventArgs object holding information about the event.</param>

        void OnMessageReceived(MessageEventArgs e);
        
/**//// <summary>
        
/// Handles a creation of a new instance of the application.
        
/// </summary>
        
/// <param name="e">The EventArgs object holding information about the event.</param>

        void OnNewInstanceCreated(EventArgs e);
    }

}

注意,由于该方法可能是在另一个进程中调用所以在其中调用主窗体时需要回到主窗体
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SingleInstancing;
using System.Runtime.InteropServices;
using Common;

namespace WindowsApplication1
{
    
public partial class Form1 : Form, ISingleInstanceEnforcer
    
{
        
private int i =1;

        
public Form1()
        
{
            InitializeComponent();
        }


        
private void Form1_Load(object sender, EventArgs e)
        
{
            SetLabel();
        }


        
ISingleInstanceEnforcer 成员#region ISingleInstanceEnforcer 成员

        
public void OnMessageReceived(MessageEventArgs e)
        
{
            
        }


        
public void OnNewInstanceCreated(EventArgs e)
        
{
            i
++;
            SetLabel();
            
        }


        
private void SetLabel()
        
{
            
this.Invoke(new MethodInvoker(delegate() {
                
this.label1.Text = i.ToString();
                
//ShowWindowAsync(this.Handle, 1);
                WindowUtility.SetActiveWindow(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
                WindowUtility.ShowWindow(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle, WindowUtility.SW_RESTORE);
                WindowUtility.SetForegroundWindow(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
            }
));
            
        }


        
#endregion

    }

}

namespace  WindowsApplication1
{
    
partial class Form1
    
{
        
/**//// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.IContainer components = null;

        
/**//// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>
        
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

        protected override void Dispose(bool disposing)
        
{
            
if (disposing && (components != null))
            
{
                components.Dispose();
            }

            
base.Dispose(disposing);
        }


        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码

        
/**//// <summary>
        
/// 设计器支持所需的方法 - 不要
        
/// 使用代码编辑器修改此方法的内容。
        
/// </summary>

        private void InitializeComponent()
        
{
            
this.label1 = new System.Windows.Forms.Label();
            
this.SuspendLayout();
            
// 
            
// label1
            
// 
            this.label1.AutoSize = true;
            
this.label1.Location = new System.Drawing.Point(127109);
            
this.label1.Name = "label1";
            
this.label1.Size = new System.Drawing.Size(1112);
            
this.label1.TabIndex = 0;
            
this.label1.Text = "1";
            
// 
            
// Form1
            
// 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            
this.ClientSize = new System.Drawing.Size(284264);
            
this.Controls.Add(this.label1);
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.Load += new System.EventHandler(this.Form1_Load);
            
this.ResumeLayout(false);
            
this.PerformLayout();

        }


        
#endregion


        
private System.Windows.Forms.Label label1;
    }

}



启动程序如下:
Code
using System;
using System.Collections.Generic;
using System.Text;
using SingleInstancing;
using System.Windows.Forms;

namespace WindowsApplication1
{
    [Serializable]
    
public class App
    
{
        
Singleton#region Singleton

        
private static App instance = new App();

        
public static App Instance
        
{
            
get
            
{
                
return instance;
            }

        }

        
#endregion


        
Fields#region Fields

        
private SingleInstanceTracker tracker = null;

        
#endregion


        
Properties#region Properties
        
public Form MainForm
        
{
            
get
            
{
                
return (Form1)tracker.Enforcer;
            }

        }

       
        
#endregion


        
Method#region Method
         
private ISingleInstanceEnforcer GetSingleInstanceEnforcer()
        
{
            
return new Form1();
        }


        
public void Dispose()
        
{
           
        }

        
#endregion

        
public void Start(string[] args)
        
{
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(
false);

            
try
            
{
                
// Attempt to create a tracker
                tracker = new SingleInstanceTracker("SingleInstanceSample"new SingleInstanceEnforcerRetriever(GetSingleInstanceEnforcer));

                
// If this is the first instance of the application, run the main form
                if (tracker.IsFirstInstance)
                
{
                    
try
                    
{
                        Form1 form 
= (Form1)tracker.Enforcer;
                        Application.Run(form);
                    }

                    
finally
                    
{
                        Dispose();
                    }

                }

                
else
                
{
                    
// This is not the first instance of the application, so do nothing but send a message to the first instance
                    if (args.Length > 0)
                    
{
                        tracker.SendMessageToFirstInstance(args);
                    }

                }

            }

            
catch (SingleInstancingException ex)
            
{
                MessageBox.Show(
"Could not create a SingleInstanceTracker object:\n" + ex.Message + "\nApplication will now terminate.\n" + ex.InnerException.ToString());

                
return;
            }

            
finally
            
{
                
if (tracker != null)
                    tracker.Dispose();
            }

        }

    }

}




具体代码:
Winform单一实例

转载于:https://www.cnblogs.com/liuke209/archive/2008/04/01/1133355.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值