From 25e56dc5819c485a3906bdae77139e3f054d975f Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Fri, 4 Sep 2015 15:12:49 +0300 Subject: [PATCH] Designer: fixed VS-embedded mode --- .../Perspex.Designer/Comm/CommChannel.cs | 26 ++++++++++++++++--- .../Perspex.Designer/Comm/ProcessHost.cs | 3 ++- src/Windows/Perspex.Designer/Program.cs | 1 + 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Windows/Perspex.Designer/Comm/CommChannel.cs b/src/Windows/Perspex.Designer/Comm/CommChannel.cs index a408298db2..67a5ca71bd 100644 --- a/src/Windows/Perspex.Designer/Comm/CommChannel.cs +++ b/src/Windows/Perspex.Designer/Comm/CommChannel.cs @@ -3,10 +3,12 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Windows.Forms; using System.Windows.Threading; namespace Perspex.Designer.Comm @@ -20,6 +22,7 @@ namespace Perspex.Designer.Comm private BlockingCollection _outputQueue = new BlockingCollection(); public event Action OnMessage; public event Action Disposed; + public event Action Exception; public CommChannel(Stream input, Stream output) { _input = new BinaryReader(input); @@ -36,7 +39,7 @@ namespace Perspex.Designer.Comm public void SendMessage(object message) { - var fmt = new BinaryFormatter(); + var fmt = GetFormatter(); var ms = new MemoryStream(); fmt.Serialize(ms, message); _outputQueue.Add(ms.ToArray()); @@ -57,6 +60,7 @@ namespace Perspex.Designer.Comm } catch (Exception e) { + Exception?.Invoke(e); Dispose(); } } @@ -73,7 +77,8 @@ namespace Perspex.Designer.Comm async void ReaderThread() { - var fmt = new BinaryFormatter(); + await Task.Delay(10).ConfigureAwait(false); + var fmt = GetFormatter(); while (!_terminating.Task.IsCompleted) { try @@ -87,15 +92,30 @@ namespace Perspex.Designer.Comm var message = fmt.Deserialize(new MemoryStream(data)); _dispatcher.Post(_ => OnMessage?.Invoke(message), null); } - catch + catch(Exception e) { Dispose(); + Exception?.Invoke(e); return; } } } + sealed class BinderFix : SerializationBinder + { + public override Type BindToType(string assemblyName, string typeName) + { + return typeof (CommChannel).Assembly.GetType(typeName, false, true); + + } + } + + BinaryFormatter GetFormatter() + { + return new BinaryFormatter() {Binder = new BinderFix()}; + } + public void Dispose() { if(_terminating.Task.IsCompleted) diff --git a/src/Windows/Perspex.Designer/Comm/ProcessHost.cs b/src/Windows/Perspex.Designer/Comm/ProcessHost.cs index 7097c0e931..0de09a033b 100644 --- a/src/Windows/Perspex.Designer/Comm/ProcessHost.cs +++ b/src/Windows/Perspex.Designer/Comm/ProcessHost.cs @@ -103,7 +103,8 @@ namespace Perspex.Designer.Comm UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, - RedirectStandardError = true + RedirectStandardError = true, + CreateNoWindow = true }, EnableRaisingEvents = true }; diff --git a/src/Windows/Perspex.Designer/Program.cs b/src/Windows/Perspex.Designer/Program.cs index af6acfba8e..6e58a3c98b 100644 --- a/src/Windows/Perspex.Designer/Program.cs +++ b/src/Windows/Perspex.Designer/Program.cs @@ -34,6 +34,7 @@ namespace Perspex.Designer Console.SetOut(new NullTextWriter()); Console.SetError(new NullTextWriter()); comm.Disposed += () => Process.GetCurrentProcess().Kill(); + comm.SendMessage(new StateMessage("Staying awhile and listening...")); var service = new PerspexAppHost(comm); service.Start(); Application.Run();