Skip to content

Commit

Permalink
Switch serializers to handle internal type
Browse files Browse the repository at this point in the history
  • Loading branch information
Oren Novotny committed Nov 20, 2018
1 parent c4bdbc7 commit c2fcfae
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
19 changes: 10 additions & 9 deletions SingleInstanceHelper/ApplicationActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -51,7 +52,7 @@ public static bool LaunchOrReturn(Action<string[]> otherInstanceCallback, string
else
{
// We are not the first instance, send the named pipe message with our payload and stop loading
var namedPipeXmlPayload = new NamedPipeXmlPayload
var namedPipeXmlPayload = new Payload
{
CommandLineArguments = Environment.GetCommandLineArgs().ToList()
};
Expand Down Expand Up @@ -87,16 +88,16 @@ private static bool IsApplicationFirstInstance()
/// Uses a named pipe to send the currently parsed options to an already running instance.
/// </summary>
/// <param name="namedPipePayload"></param>
private static void NamedPipeClientSendOptions(NamedPipeXmlPayload namedPipePayload)
private static void NamedPipeClientSendOptions(Payload namedPipePayload)
{
try
{
using (var namedPipeClientStream = new NamedPipeClientStream(".", GetPipeName(), PipeDirection.Out))
{
namedPipeClientStream.Connect(3000); // Maximum wait 3 seconds

var xmlSerializer = new XmlSerializer(typeof(NamedPipeXmlPayload));
xmlSerializer.Serialize(namedPipeClientStream, namedPipePayload);
var ser = new DataContractJsonSerializer(typeof(Payload));
ser.WriteObject(namedPipeClientStream, namedPipePayload);
}
}
catch (Exception)
Expand Down Expand Up @@ -134,17 +135,17 @@ private static void NamedPipeServerConnectionCallback(IAsyncResult iAsyncResult)
// End waiting for the connection
_namedPipeServerStream.EndWaitForConnection(iAsyncResult);

var xmlSerializer = new XmlSerializer(typeof(NamedPipeXmlPayload));
var namedPipeXmlPayload = (NamedPipeXmlPayload)xmlSerializer.Deserialize(_namedPipeServerStream);
var ser = new DataContractJsonSerializer(typeof(Payload));
var payload = (Payload)ser.ReadObject(_namedPipeServerStream);

// namedPipeXmlPayload contains the data sent from the other instance
// payload contains the data sent from the other instance
if (_syncContext != null)
{
_syncContext.Post(_ => _otherInstanceCallback(namedPipeXmlPayload.CommandLineArguments.ToArray()), null);
_syncContext.Post(_ => _otherInstanceCallback(payload.CommandLineArguments.ToArray()), null);
}
else
{
_otherInstanceCallback(namedPipeXmlPayload.CommandLineArguments.ToArray());
_otherInstanceCallback(payload.CommandLineArguments.ToArray());
}
}
catch (ObjectDisposedException)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Serialization;

namespace SingleInstanceHelper
{
internal class NamedPipeXmlPayload
[DataContract]
internal class Payload
{
/// <summary>
/// A list of command line arguments.
/// </summary>
[XmlElement("CommandLineArguments")]
[DataMember]
public List<string> CommandLineArguments { get; set; } = new List<string>();
}
}
2 changes: 1 addition & 1 deletion WinFormsTestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void Main(string[] args)
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

var first = ApplicationActivator.LaunchOrReturn(otherInstance => { MessageBox.Show("got data"); }, args);
var first = ApplicationActivator.LaunchOrReturn(otherInstance => { MessageBox.Show("got data: " + otherInstance.Skip(1).FirstOrDefault()); }, args);
if(!first)
return;
Application.Run(new Form1());
Expand Down

0 comments on commit c2fcfae

Please sign in to comment.