/// <summary> /// The function called when a client connects to the named pipe. /// </summary> /// <param name="iAsyncResult"></param> private void NamedPipeServerConnectionCallback(IAsyncResult iAsyncResult) { try { // End waiting for the connection _namedPipeServerStream.EndWaitForConnection(_namedPipeAsyncResult); // Read data and prevent access to _namedPipeXmlPayload during threaded operations lock (_namedPiperServerThreadLock) { // Read data from client var xmlSerializer = new XmlSerializer(typeof(NamedPipeXmlPayload)); _namedPipeXmlPayload = (NamedPipeXmlPayload)xmlSerializer.Deserialize(_namedPipeServerStream); // Need to signal quit? if (_namedPipeXmlPayload.SignalQuit) { _eventShutdownRequested.Set(); return; } // Flag that there is new options data available. It will be applied on timer event // when the tools menus are NOT open _eventNewOptionsAvailable.Set(); } } catch (ObjectDisposedException) { // EndWaitForConnection will exception when someone calls .Close() before a connection received // In that case we dont create any more pipes and just return // This will happen when app is closing and our pipe is closed return; } catch (Exception) { // ignored } finally { // Close the original server (we have to create a new one each time) if (_namedPipeServerStream != null) { _namedPipeServerStream.Dispose(); _namedPipeServerStream = null; } _namedPipeAsyncResult = null; } // Create a new pipe for next connection NamedPipeServerCreateServer(); }
/// <summary> /// Uses a named pipe to send the currently parsed options to an already running instance. /// </summary> /// <param name="namedPipePayload"></param> private void NamedPipeClientSendOptions(NamedPipeXmlPayload namedPipePayload) { //List<string> arguments = Environment.GetCommandLineArgs().ToList(); try { using (var namedPipeClientStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out)) { namedPipeClientStream.Connect(3000); // Maximum wait 3 seconds var xmlSerializer = new XmlSerializer(typeof(NamedPipeXmlPayload)); xmlSerializer.Serialize(namedPipeClientStream, namedPipePayload); } } catch (Exception) { // Error connecting or sending } }
/// <summary> /// Processes command line options and options.xml /// </summary> /// <returns></returns> private bool OptionsReadCommandLineAndOptionsFile() { string[] arguments = Environment.GetCommandLineArgs(); string optionsFilename = string.Empty; if (arguments.Length >= 2) { string arg = arguments[1].ToUpper(); if (arg == "/?" || arg == "?") { var usage = @"AutoIt.OSD.Background [/Close] | [Options.xml]"; MessageBox.Show(usage, Text, MessageBoxButtons.OK, MessageBoxIcon.Information); return(false); } if (arg == "/CLOSE" || arg == "CLOSE") { // If we are not the first instance, send a quit message along the pipe if (!IsApplicationFirstInstance()) { //KillPreviousInstance(); var namedPipeXmlPayload = new NamedPipeXmlPayload { SignalQuit = true }; NamedPipeClientSendOptions(namedPipeXmlPayload); } return(false); } // Get the options filename optionsFilename = arguments[1]; } // If no filename specified, use options.xml in current folder if (arguments.Length == 1) { optionsFilename = _appPath + @"\Options.xml"; } try { var deSerializer = new XmlSerializer(typeof(Options)); TextReader reader = new StreamReader(optionsFilename); _options = (Options)deSerializer.Deserialize(reader); _options.SanityCheck(); } catch (Exception e) { string message = Resources.UnableToParseXml; if (e.InnerException != null) { message += "\n\n" + e.InnerException.Message; } MessageBox.Show(message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } // Additional parsing of some string values in useful fields if (!OptionsXmlOptionsObjectToFields(_options)) { return(false); } // If are not the first instance then send a message to the running app with the new options and then quit if (!IsApplicationFirstInstance()) { var namedPipeXmlPayload = new NamedPipeXmlPayload { SignalQuit = false, OsdBackgroundDir = _osdBackgroundDir, OsdBackgroundWorkingDir = Directory.GetCurrentDirectory(), Options = _options }; NamedPipeClientSendOptions(namedPipeXmlPayload); return(false); } return(true); }