日期:2014-05-17  浏览次数:20901 次

C#中如何在main函数中实现单例模式
我描述可能不是那么准确。。
现在我们要实现在命令行多次执行某一程序时,该程序只会执行出现一次,每次运行的不同结果会以选项卡的形式出现在程序窗口中,就像这样

目前我已经可以做到识别出是否是第一次运行该程序,但不会实现每当再运行一次该程序时,如何动态添加一个tabpage,(通过button什么的添加我会,但是直接在运行程序的时候就自动添加我不太会)...
谢谢大家

------解决方案--------------------
	interface IRemoteApplicationInterface
{
void RemoteApplicationNotification();
}

class RemotableApplication : MarshalByRefObject, IRemoteApplicationInterface
{
/// <summary>
/// Start for remote application notification.
/// </summary>
/// <param name="name">remote name.</param>
/// <param name="action">remote notification action.</param>
public static void RemoteApplicationNotification(string name, Action action)
{
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(new System.Runtime.Remoting.Channels.Ipc.IpcChannel(name + ".IPC"), false);
System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotableApplication), "Launch", System.Runtime.Remoting.WellKnownObjectMode.SingleCall);
System.Diagnostics.Debug.Assert(remoteApplicationNotificationAction == null);
remoteApplicationNotificationAction = action;
}

/// <summary>
/// call the running application to do...
/// </summary>
/// <param name="name">remote name.</param>
public static void NotificationRemoteApplication(string name)
{
var ra = Activator.GetObject(typeof(IRemoteApplicationInterface), "ipc://" + name + ".IPC/Launch") as IRemoteApplicationInterface;
if (ra != null)
{
ra.RemoteApplicationNotification();
}
}

#region IRemoteApplicationInterface 成员

static Action remoteApplicationNotificationAction = null;
void IRemoteApplicationInterface.RemoteApplicationNotification()
{
if (remoteApplicationNotificationAction != null)
{
remoteApplicationNotificationAction();
}
}

#endregion
}

static class UniqueEntry
{
public static void Run(string name, Action run, Action notification, Func<bool> notify)
{
bool created;
string mname = Environment.UserName + "." + name;
var mutex = new System.Threading.Mutex(true, @"Local\" + mname, out created);
if (!created)
{ // 程序在运行中。
if (notify())
{
try
{
RemotableApplication.NotificationRemoteApplication(mname);
}
catch
{
}
}
return;
}
else
{
try
{
RemotableAppl