日期:2014-05-17 浏览次数:20941 次
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