# HG changeset patch # User sl # Date 1408093877 -7200 # Node ID f19b04646b6ab7aae9359a6560fca275d7477270 # Parent c375286d1a1c51f7eb47e3d123d52adfbb239935 Fixing our client issue with static MainForm overwritten when using multiple clients. That was basically our issue with broadcast not working the way it should. diff -r c375286d1a1c -r f19b04646b6a Client/Client.cs --- a/Client/Client.cs Fri Aug 15 10:20:01 2014 +0200 +++ b/Client/Client.cs Fri Aug 15 11:11:17 2014 +0200 @@ -14,9 +14,16 @@ /// /// /// - [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)] + [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] public class Callback : IDisplayServiceCallback, IDisposable { + private MainForm MainForm { get; set; } + + public Callback(MainForm aMainForm) + { + MainForm = aMainForm; + } + public void OnConnected() { //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread); @@ -32,8 +39,8 @@ //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId); //MessageBox.Show("OnServerClosing()", "Client"); - Program.iMainForm.CloseConnection(); - Program.iMainForm.Close(); + MainForm.CloseConnectionThreadSafe(); + MainForm.CloseThreadSafe(); } //From IDisposable @@ -47,7 +54,7 @@ /// /// /// - [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)] + [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] public class Client : DuplexClientBase { public string Name { get; set; } diff -r c375286d1a1c -r f19b04646b6a Client/MainForm.cs --- a/Client/MainForm.cs Fri Aug 15 10:20:01 2014 +0200 +++ b/Client/MainForm.cs Fri Aug 15 11:11:17 2014 +0200 @@ -9,6 +9,7 @@ using System.Windows.Forms; using System.ServiceModel; using System.ServiceModel.Channels; +using System.Diagnostics; namespace SharpDisplayClient @@ -32,7 +33,7 @@ private void MainForm_Load(object sender, EventArgs e) { - iCallback = new Callback(); + iCallback = new Callback(this); //Instance context is then managed by our client class InstanceContext instanceContext = new InstanceContext(iCallback); iClient = new Client(instanceContext); @@ -45,18 +46,57 @@ } - public void CloseConnection() + + + public delegate void CloseConnectionDelegate(); + public delegate void CloseDelegate(); + + /// + /// + /// + public void CloseConnectionThreadSafe() { - if (IsClientReady()) + if (this.InvokeRequired) { - //iClient.Disconnect(); - iClient.Close(); + //Not in the proper thread, invoke ourselves + CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe); + this.Invoke(d, new object[] { }); } + else + { + //We are in the proper thread + if (IsClientReady()) + { + //iClient.Disconnect(); + Trace.TraceInformation("Closing client: " + iClient.SessionId); + iClient.Close(); + Trace.TraceInformation("Closed client: " + iClient.SessionId); + } - iClient = null; - iCallback = null; + iClient = null; + iCallback = null; + } } + /// + /// + /// + public void CloseThreadSafe() + { + if (this.InvokeRequired) + { + //Not in the proper thread, invoke ourselves + CloseDelegate d = new CloseDelegate(CloseThreadSafe); + this.Invoke(d, new object[] { }); + } + else + { + //We are in the proper thread + Close(); + } + } + + private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (IsClientReady()) //Could catch exception instead @@ -64,7 +104,7 @@ iClient.Disconnect(); } - CloseConnection(); + CloseConnectionThreadSafe(); } public bool IsClientReady() diff -r c375286d1a1c -r f19b04646b6a Client/Program.cs --- a/Client/Program.cs Fri Aug 15 10:20:01 2014 +0200 +++ b/Client/Program.cs Fri Aug 15 11:11:17 2014 +0200 @@ -8,8 +8,6 @@ { static public class Program { - public static MainForm iMainForm; - /// /// The main entry point for the application. /// @@ -21,8 +19,7 @@ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - iMainForm = new MainForm(); - Application.Run(iMainForm); + Application.Run(new MainForm()); } } } diff -r c375286d1a1c -r f19b04646b6a Server/MainForm.cs --- a/Server/MainForm.cs Fri Aug 15 10:20:01 2014 +0200 +++ b/Server/MainForm.cs Fri Aug 15 11:11:17 2014 +0200 @@ -12,6 +12,7 @@ using System.Drawing.Imaging; using System.ServiceModel; using System.Threading; +using System.Diagnostics; // using SharpDisplayInterface; using SharpDisplayClient; @@ -386,6 +387,8 @@ public void BroadcastCloseEvent() { + Trace.TraceInformation("BroadcastCloseEvent - start"); + var inactiveClients = new List(); foreach (var client in iClients) { @@ -393,6 +396,7 @@ { try { + Trace.TraceInformation("BroadcastCloseEvent - " + client.Key); client.Value.OnServerClosing(/*eventData*/); } catch (Exception ex) diff -r c375286d1a1c -r f19b04646b6a Server/Program.cs --- a/Server/Program.cs Fri Aug 15 10:20:01 2014 +0200 +++ b/Server/Program.cs Fri Aug 15 11:11:17 2014 +0200 @@ -8,6 +8,8 @@ { static class Program { + //WARNING: This is assuming we have a single instance of our program. + //That is what we want but we should enforce it somehow. public static MainForm iMainForm; /// /// The main entry point for the application.