Skip to content

Commit c2fcfae

Browse files
author
Oren Novotny
committed
Switch serializers to handle internal type
1 parent c4bdbc7 commit c2fcfae

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

SingleInstanceHelper/ApplicationActivator.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.IO.Pipes;
44
using System.Linq;
5+
using System.Runtime.Serialization.Json;
56
using System.Security.Cryptography;
67
using System.Text;
78
using System.Threading;
@@ -51,7 +52,7 @@ public static bool LaunchOrReturn(Action<string[]> otherInstanceCallback, string
5152
else
5253
{
5354
// We are not the first instance, send the named pipe message with our payload and stop loading
54-
var namedPipeXmlPayload = new NamedPipeXmlPayload
55+
var namedPipeXmlPayload = new Payload
5556
{
5657
CommandLineArguments = Environment.GetCommandLineArgs().ToList()
5758
};
@@ -87,16 +88,16 @@ private static bool IsApplicationFirstInstance()
8788
/// Uses a named pipe to send the currently parsed options to an already running instance.
8889
/// </summary>
8990
/// <param name="namedPipePayload"></param>
90-
private static void NamedPipeClientSendOptions(NamedPipeXmlPayload namedPipePayload)
91+
private static void NamedPipeClientSendOptions(Payload namedPipePayload)
9192
{
9293
try
9394
{
9495
using (var namedPipeClientStream = new NamedPipeClientStream(".", GetPipeName(), PipeDirection.Out))
9596
{
9697
namedPipeClientStream.Connect(3000); // Maximum wait 3 seconds
9798

98-
var xmlSerializer = new XmlSerializer(typeof(NamedPipeXmlPayload));
99-
xmlSerializer.Serialize(namedPipeClientStream, namedPipePayload);
99+
var ser = new DataContractJsonSerializer(typeof(Payload));
100+
ser.WriteObject(namedPipeClientStream, namedPipePayload);
100101
}
101102
}
102103
catch (Exception)
@@ -134,17 +135,17 @@ private static void NamedPipeServerConnectionCallback(IAsyncResult iAsyncResult)
134135
// End waiting for the connection
135136
_namedPipeServerStream.EndWaitForConnection(iAsyncResult);
136137

137-
var xmlSerializer = new XmlSerializer(typeof(NamedPipeXmlPayload));
138-
var namedPipeXmlPayload = (NamedPipeXmlPayload)xmlSerializer.Deserialize(_namedPipeServerStream);
138+
var ser = new DataContractJsonSerializer(typeof(Payload));
139+
var payload = (Payload)ser.ReadObject(_namedPipeServerStream);
139140

140-
// namedPipeXmlPayload contains the data sent from the other instance
141+
// payload contains the data sent from the other instance
141142
if (_syncContext != null)
142143
{
143-
_syncContext.Post(_ => _otherInstanceCallback(namedPipeXmlPayload.CommandLineArguments.ToArray()), null);
144+
_syncContext.Post(_ => _otherInstanceCallback(payload.CommandLineArguments.ToArray()), null);
144145
}
145146
else
146147
{
147-
_otherInstanceCallback(namedPipeXmlPayload.CommandLineArguments.ToArray());
148+
_otherInstanceCallback(payload.CommandLineArguments.ToArray());
148149
}
149150
}
150151
catch (ObjectDisposedException)
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Runtime.Serialization;
34
using System.Text;
45
using System.Xml.Serialization;
56

67
namespace SingleInstanceHelper
78
{
8-
internal class NamedPipeXmlPayload
9+
[DataContract]
10+
internal class Payload
911
{
1012
/// <summary>
1113
/// A list of command line arguments.
1214
/// </summary>
13-
[XmlElement("CommandLineArguments")]
15+
[DataMember]
1416
public List<string> CommandLineArguments { get; set; } = new List<string>();
1517
}
1618
}

WinFormsTestApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static void Main(string[] args)
1818
Application.EnableVisualStyles();
1919
Application.SetCompatibleTextRenderingDefault(false);
2020

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

0 commit comments

Comments
 (0)