From 886faae85be3d77a9a11b9395ea21cc498a6395e Mon Sep 17 00:00:00 2001
From: Adam Thorn <alt36@cam.ac.uk>
Date: Tue, 16 Jun 2020 16:36:14 +0100
Subject: [PATCH] Have clients unregister from server on exit

---
 WpkgInstaller/IWpkgNotifierMessageServer.cs |  3 +++
 WpkgInstaller/WpkgInstaller.cs              | 11 +++++++++++
 WpkgNotifier/TrayNotifier.cs                | 17 ++++++++++++++---
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/WpkgInstaller/IWpkgNotifierMessageServer.cs b/WpkgInstaller/IWpkgNotifierMessageServer.cs
index 3dd5380..b1ec47c 100755
--- a/WpkgInstaller/IWpkgNotifierMessageServer.cs
+++ b/WpkgInstaller/IWpkgNotifierMessageServer.cs
@@ -9,6 +9,9 @@ namespace WpkgInstaller
         [OperationContract(IsOneWay = true)]
         void Register(int pid);
 
+        [OperationContract(IsOneWay = true)]
+        void Unregister(int pid);
+
         [OperationContract(IsOneWay = true)]
         void ProcessRequest(WpkgMessageRequestType type);
     }
diff --git a/WpkgInstaller/WpkgInstaller.cs b/WpkgInstaller/WpkgInstaller.cs
index 9de8c69..31f161b 100644
--- a/WpkgInstaller/WpkgInstaller.cs
+++ b/WpkgInstaller/WpkgInstaller.cs
@@ -207,6 +207,17 @@ namespace WpkgInstaller
             }
         }
 
+        public void Unregister(int pid)
+        {
+            if(clients.Remove(pid))
+            {
+                eventLog.WriteEntry(String.Format("Client {0} unregistered", pid));
+            }
+            else
+            {
+                eventLog.WriteEntry(String.Format("Unknown client {0} requested to unregister", pid), EventLogEntryType.Warning);
+            }
+        }
         public void ProcessRequest(WpkgMessageRequestType type)
         {
             switch (type)
diff --git a/WpkgNotifier/TrayNotifier.cs b/WpkgNotifier/TrayNotifier.cs
index d097ee7..64855fa 100644
--- a/WpkgNotifier/TrayNotifier.cs
+++ b/WpkgNotifier/TrayNotifier.cs
@@ -15,10 +15,13 @@ namespace WpkgNotifier
         private EventLog eventLog;
         private WpkgMessageState state;
         private IWpkgNotifierMessageServer server;
+        private int pid;
 
         public TrayNotifier()
         {
-
+            Process currentProcess = Process.GetCurrentProcess();
+            pid = currentProcess.Id;
+    
             eventLog = new EventLog();
             string eventSourceName = "TrayNotifier";
             string logName = "WpkgNotifier";
@@ -34,6 +37,8 @@ namespace WpkgNotifier
             
             OpenMessageChannel();
 
+            Application.ApplicationExit += new EventHandler(this.OnApplicationExit);
+
             MenuItem actionsMenu = new MenuItem("Actions");
             MenuItem syncMenuItem = new MenuItem("Sync", new EventHandler(RequestSync));
             MenuItem queryMenuItem = new MenuItem("Query", new EventHandler(RequestQuery));
@@ -65,6 +70,11 @@ namespace WpkgNotifier
             };
         }
 
+        private void OnApplicationExit(object sender, EventArgs e)
+        {
+            server.Unregister(pid);
+        }
+
         private void OpenMessageChannel()
         {
             eventLog.WriteEntry("Trying to open channel");
@@ -78,8 +88,7 @@ namespace WpkgNotifier
 
                 server = channelFactory.CreateChannel();
                 eventLog.WriteEntry("channel created");
-                Process currentProcess = Process.GetCurrentProcess();
-                server.Register(currentProcess.Id);
+                server.Register(pid);
                 eventLog.WriteEntry("Registered with server");
             }
             catch (Exception e)
@@ -114,6 +123,8 @@ namespace WpkgNotifier
             Application.Exit();
         }
 
+
+
         public void SetToolTip(string msg)
         {
             notifyIcon.Text = msg;
-- 
GitLab