From 1ca0fe25c2d589159614e7cfe85392d07ce8848c Mon Sep 17 00:00:00 2001 From: TyrWang Date: Fri, 17 Nov 2017 18:16:07 +0800 Subject: [PATCH 1/4] put the whole progress in a thread and only wait for 5 seconds - put ManagementObject in a "try catch" to prevent a ManagementException cause by WMI error - give SystemManufacturer and ModelName an init-value --- .../Properties/AssemblyInfo.cs | 4 +-- .../Utility/DeviceInformationService.cs | 30 ++++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Library/GoogleAnalyticsClientDotNet.Net45/Properties/AssemblyInfo.cs b/Library/GoogleAnalyticsClientDotNet.Net45/Properties/AssemblyInfo.cs index c5d3523..6ad393a 100644 --- a/Library/GoogleAnalyticsClientDotNet.Net45/Properties/AssemblyInfo.cs +++ b/Library/GoogleAnalyticsClientDotNet.Net45/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.22")] -[assembly: AssemblyFileVersion("1.0.0.22")] +[assembly: AssemblyVersion("1.0.0.23")] +[assembly: AssemblyFileVersion("1.0.0.23")] diff --git a/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs b/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs index 0c2c09d..17dcb38 100644 --- a/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs +++ b/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Management; using System.Windows.Input; +using System.Threading; namespace GoogleAnalyticsClientDotNet.Utility { @@ -31,7 +32,7 @@ public bool IsTouchEnabled public string ModelName { get; private set; - } + } = "Not found"; public string Name { @@ -56,7 +57,7 @@ public string SystemArchitecture public string SystemManufacturer { get; private set; - } + } = "Not found"; private static string uniqueId; public string UniqueId @@ -137,13 +138,26 @@ public DeviceInformationService() //initialize the searcher with the query it is supposed to execute using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)) { - //execute the query - foreach (System.Management.ManagementObject process in searcher.Get()) + try { - //print system info - process.Get(); - SystemManufacturer = $"{process["Manufacturer"]}"; - ModelName = $"{process["Model"]}"; + //execute the query + ManualResetEvent mre = new ManualResetEvent(false); + Thread waitingThread = new Thread(() => { + foreach (System.Management.ManagementObject process in searcher.Get()) + { + //print system info + process.Get(); + SystemManufacturer = $"{process["Manufacturer"]}"; + ModelName = $"{process["Model"]}"; + } + mre.Set(); + }); + waitingThread.Start(); + mre.WaitOne(5000); // wait 5 seconds + } + catch (Exception) + { + } } } From 76e634e19d17181c22999f74e92a9314d3d5b00a Mon Sep 17 00:00:00 2001 From: TyrWang Date: Fri, 17 Nov 2017 18:19:22 +0800 Subject: [PATCH 2/4] fix coding style --- .../Utility/DeviceInformationService.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs b/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs index 17dcb38..5b85581 100644 --- a/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs +++ b/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs @@ -1,9 +1,9 @@ -using Microsoft.Win32; -using System; +using System; using System.Linq; using System.Management; -using System.Windows.Input; using System.Threading; +using System.Windows.Input; +using Microsoft.Win32; namespace GoogleAnalyticsClientDotNet.Utility { @@ -142,8 +142,9 @@ public DeviceInformationService() { //execute the query ManualResetEvent mre = new ManualResetEvent(false); - Thread waitingThread = new Thread(() => { - foreach (System.Management.ManagementObject process in searcher.Get()) + Thread waitingThread = new Thread(() => + { + foreach (ManagementObject process in searcher.Get()) { //print system info process.Get(); From 1fdc6b23fbd31685be70107d90bf61eb7ee68185 Mon Sep 17 00:00:00 2001 From: TyrWang Date: Mon, 20 Nov 2017 12:10:43 +0800 Subject: [PATCH 3/4] avoid custom sting content --- .../Utility/DeviceInformationService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs b/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs index 5b85581..6296d06 100644 --- a/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs +++ b/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs @@ -32,7 +32,7 @@ public bool IsTouchEnabled public string ModelName { get; private set; - } = "Not found"; + } = string.Empty; public string Name { @@ -57,7 +57,7 @@ public string SystemArchitecture public string SystemManufacturer { get; private set; - } = "Not found"; + } = string.Empty; private static string uniqueId; public string UniqueId From 5ab1925f416f7712af0715a51b168f8d750c2ee5 Mon Sep 17 00:00:00 2001 From: TyrWang Date: Mon, 20 Nov 2017 19:05:25 +0800 Subject: [PATCH 4/4] use Task to prevent blocking the UI thread --- .../Utility/DeviceInformationService.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs b/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs index 6296d06..bb126d4 100644 --- a/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs +++ b/Library/GoogleAnalyticsClientDotNet.Net45/Utility/DeviceInformationService.cs @@ -1,7 +1,7 @@ using System; using System.Linq; using System.Management; -using System.Threading; +using System.Threading.Tasks; using System.Windows.Input; using Microsoft.Win32; @@ -140,9 +140,7 @@ public DeviceInformationService() { try { - //execute the query - ManualResetEvent mre = new ManualResetEvent(false); - Thread waitingThread = new Thread(() => + Task.Run(() => { foreach (ManagementObject process in searcher.Get()) { @@ -151,10 +149,8 @@ public DeviceInformationService() SystemManufacturer = $"{process["Manufacturer"]}"; ModelName = $"{process["Model"]}"; } - mre.Set(); - }); - waitingThread.Start(); - mre.WaitOne(5000); // wait 5 seconds + } + ); } catch (Exception) {