1.1 --- a/Server/MainForm.cs Fri Aug 15 11:11:17 2014 +0200
1.2 +++ b/Server/MainForm.cs Fri Aug 15 13:26:38 2014 +0200
1.3 @@ -357,6 +357,7 @@
1.4 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
1.5 {
1.6 StopServer();
1.7 + e.Cancel = iClosing;
1.8 }
1.9
1.10 public void StartServer()
1.11 @@ -373,16 +374,24 @@
1.12
1.13 public void StopServer()
1.14 {
1.15 - //Tell connected client first? Is that possible?
1.16 -
1.17 - if (iClients.Count>0)
1.18 + if (iClients.Count > 0 && !iClosing)
1.19 {
1.20 //Tell our clients
1.21 + iClosing = true;
1.22 BroadcastCloseEvent();
1.23 }
1.24 -
1.25 - //iServiceHost.Close();
1.26 -
1.27 + else if (iClosing)
1.28 + {
1.29 + if (MessageBox.Show("Force exit?", "Waiting for clients...", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
1.30 + {
1.31 + iClosing = false; //We make sure we force close if asked twice
1.32 + }
1.33 + }
1.34 + else
1.35 + {
1.36 + //We removed that as it often lags for some reason
1.37 + //iServiceHost.Close();
1.38 + }
1.39 }
1.40
1.41 public void BroadcastCloseEvent()
1.42 @@ -397,7 +406,7 @@
1.43 try
1.44 {
1.45 Trace.TraceInformation("BroadcastCloseEvent - " + client.Key);
1.46 - client.Value.OnServerClosing(/*eventData*/);
1.47 + client.Value.OnCloseOrder(/*eventData*/);
1.48 }
1.49 catch (Exception ex)
1.50 {
1.51 @@ -449,6 +458,7 @@
1.52 public delegate void RemoveClientDelegate(string aSessionId);
1.53 public delegate void SetTextDelegate(int aLineIndex, string aText);
1.54 public delegate void SetTextsDelegate(System.Collections.Generic.IList<string> aTexts);
1.55 + public delegate void SetClientNameDelegate(string aSessionId, string aName);
1.56
1.57
1.58 /// <summary>
1.59 @@ -494,7 +504,16 @@
1.60 {
1.61 Program.iMainForm.iClients.Remove(aSessionId);
1.62 Program.iMainForm.treeViewClients.Nodes.Remove(Program.iMainForm.treeViewClients.Nodes.Find(aSessionId, false)[0]);
1.63 - }
1.64 + }
1.65 +
1.66 + if (iClosing && iClients.Count == 0)
1.67 + {
1.68 + //We were closing our form
1.69 + //All clients are now closed
1.70 + //Just resume our close operation
1.71 + iClosing = false;
1.72 + Close();
1.73 + }
1.74 }
1.75 }
1.76
1.77 @@ -554,7 +573,38 @@
1.78 }
1.79 }
1.80 }
1.81 + }
1.82
1.83 +
1.84 + /// <summary>
1.85 + ///
1.86 + /// </summary>
1.87 + /// <param name="aSessionId"></param>
1.88 + /// <param name="aName"></param>
1.89 + public void SetClientNameThreadSafe(string aSessionId, string aName)
1.90 + {
1.91 + if (this.InvokeRequired)
1.92 + {
1.93 + //Not in the proper thread, invoke ourselves
1.94 + SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe);
1.95 + this.Invoke(d, new object[] { aSessionId, aName });
1.96 + }
1.97 + else
1.98 + {
1.99 + //We are in the proper thread
1.100 + //Remove this session from both client collection and UI tree view
1.101 + if (Program.iMainForm.iClients.Keys.Contains(aSessionId))
1.102 + {
1.103 + //Change our session node text
1.104 + TreeNode node = Program.iMainForm.treeViewClients.Nodes.Find(aSessionId, false)[0];
1.105 + node.Text = aName;
1.106 + //Add a child with SessionId
1.107 + node.Nodes.Add(new TreeNode(aSessionId));
1.108 +
1.109 + //Program.iMainForm.iClients.Remove(aSessionId);
1.110 + //Program.iMainForm.treeViewClients.Nodes.Remove(Program.iMainForm.treeViewClients.Nodes.Find(aSessionId, false)[0]);
1.111 + }
1.112 + }
1.113 }
1.114
1.115 }