/// <summary> /// Creates a client channel and obtains a reference to the remoting service exposed by the /// server - in this case, the remoting service exposed by the first instance. Calls a function /// of the remoting service class to pass on command line arguments from the second instance to /// the first and cause it to activate itself. /// </summary> /// <param name="channelName">The application's IPC channel name.</param> /// <param name="args"> /// Command line arguments for the second instance, passed to the first instance to take /// appropriate action. /// </param> /// <returns> /// <see langword="true"/> if the first instance was successfully notified; /// <see langword="false"/> if the first instance could not be reached. /// </returns> private static bool SignalFirstInstance(string channelName, string[] args) { IpcClientChannel secondInstanceChannel = new IpcClientChannel(); ChannelServices.RegisterChannel(secondInstanceChannel, true); string remotingServiceUrl = IpcProtocol + channelName + "/" + RemoteServiceName; // Obtain a reference to the remoting service exposed by the server i.e the first instance of the application. IpcRemoteService firstInstanceRemoteServiceReference = (IpcRemoteService)RemotingServices.Connect(typeof(IpcRemoteService), remotingServiceUrl); // Check that the remote service exists, in some cases the first instance may not yet have // created one, in which case we wait a few milliseconds and retry. for (int i = 0; i < 40 && firstInstanceRemoteServiceReference == null; i++) { Thread.Sleep(25); } if (firstInstanceRemoteServiceReference == null) { // Remove service not found. return(false); } // Invoke a method of the remote service exposed by the first instance passing on the // command line arguments and causing the first instance to activate itself. // The remove service call may fail if the pipe is busy (e.g. if multiple instances // are opened at once.) bool success = false; for (int i = 0; i < 100 && !success; i++) { try { firstInstanceRemoteServiceReference.InvokeFirstInstance(args); success = true; } catch { // Remove service call failed. Pipe is probably busy. // --> Wait and retry. Thread.Sleep(25); } } return(success); }
private static void CreateRemoteService(string channelName) { var serverProvider = new BinaryServerFormatterSinkProvider(); serverProvider.TypeFilterLevel = TypeFilterLevel.Full; IDictionary props = new Dictionary <string, string>(); props["name"] = channelName; props["portName"] = channelName; props["exclusiveAddressUse"] = "false"; _channel = new IpcServerChannel(props, serverProvider); ChannelServices.RegisterChannel(_channel, true); var remoteService = new IpcRemoteService(); RemotingServices.Marshal(remoteService, RemoteServiceName); }
/// <summary> /// Creates a client channel and obtains a reference to the remoting service exposed by the server - /// in this case, the remoting service exposed by the first instance. Calls a function of the remoting service /// class to pass on command line arguments from the second instance to the first and cause it to activate itself. /// </summary> /// <param name="channelName">Application's IPC channel name.</param> /// <param name="args"> /// Command line arguments for the second instance, passed to the first instance to take appropriate action. /// </param> private static void SignalFirstInstance(string channelName, IList <string> args) { IpcClientChannel secondInstanceChannel = new IpcClientChannel(); ChannelServices.RegisterChannel(secondInstanceChannel, true); string remotingServiceUrl = IpcProtocol + channelName + "/" + RemoteServiceName; // Obtain a reference to the remoting service exposed by the server i.e the first instance of the application IpcRemoteService firstInstanceRemoteServiceReference = (IpcRemoteService)RemotingServices.Connect(typeof(IpcRemoteService), remotingServiceUrl); // Check that the remote service exists, in some cases the first instance may not yet have created one, in which case // the second instance should just exit if (firstInstanceRemoteServiceReference != null) { // Invoke a method of the remote service exposed by the first instance passing on the command line // arguments and causing the first instance to activate itself firstInstanceRemoteServiceReference.InvokeFirstInstance(args); } }
/// <summary> /// Creates a remote service for communication. /// </summary> /// <param name="channelName"> Application's IPC channel name. </param> private static void CreateRemoteService(string channelName) { var serverProvider = new BinaryServerFormatterSinkProvider { TypeFilterLevel = TypeFilterLevel.Full }; IDictionary props = new Dictionary <string, string>(); props["name"] = channelName; props["portName"] = channelName; props["exclusiveAddressUse"] = "false"; // Create the IPC Server channel with the channel properties channel = new IpcServerChannel(props, serverProvider); // Register the channel with the channel services ChannelServices.RegisterChannel(channel, true); // Expose the remote service with the 'RemoteServiceName' var remoteService = new IpcRemoteService(); RemotingServices.Marshal(remoteService, RemoteServiceName); }
/// <summary> /// Creates a remote service for communication. /// </summary> private static void CreateRemoteService() { BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider { TypeFilterLevel = TypeFilterLevel.Full }; IDictionary props = new Dictionary<string, string>(); props["name"] = _channelName; props["portName"] = _channelName; props["exclusiveAddressUse"] = "false"; // Create the IPC Server channel with the channel properties. _channel = new IpcServerChannel(props, serverProvider); // Register the channel with the channel services. ChannelServices.RegisterChannel(_channel, true); // Expose the remote service with the RemoteServiceName. IpcRemoteService remoteService = new IpcRemoteService(); RemotingServices.Marshal(remoteService, RemoteServiceName); }