From 0bef898678c57549d8f226fb2ea817f1e32ab3cd Mon Sep 17 00:00:00 2001 From: qinyuhang Date: Thu, 26 Jan 2017 00:42:46 +0800 Subject: [PATCH] Using GCD to manage pingserver Using GCD to manage pingserver, It should be after than before :smiley: --- ShadowsocksX-NG/AppDelegate.swift | 16 ---------------- ShadowsocksX-NG/PingClient.swift | 27 +++++++++++++++++---------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/ShadowsocksX-NG/AppDelegate.swift b/ShadowsocksX-NG/AppDelegate.swift index 6321403..af72ba6 100644 --- a/ShadowsocksX-NG/AppDelegate.swift +++ b/ShadowsocksX-NG/AppDelegate.swift @@ -59,23 +59,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele func applicationDidFinishLaunching(_ aNotification: Notification) { // Insert code here to initialize your application -// PingServers.instance.ping() -// let newInstance = PingTest.init(hostName: "www.baidu.com") -// newInstance.start() -// let SerMgr = ServerProfileManager.instance -// let pingServerQueue : DispatchQueue = DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.high) -// -// for profile in SerMgr.profiles { -// let host = profile.serverHost -// -// pingServerQueue.async(execute: { -// print(profile.serverHost) -// SimplePingClient().pingHostname(host) { latency in -// print("-----------\(host) latency is \(latency ?? "fail")")} -//// let pingInstance = PingTest.init(hostName: host) -//// pingInstance.start() -// })} NSUserNotificationCenter.default.delegate = self // Prepare ss-local diff --git a/ShadowsocksX-NG/PingClient.swift b/ShadowsocksX-NG/PingClient.swift index 189a742..b8080b4 100644 --- a/ShadowsocksX-NG/PingClient.swift +++ b/ShadowsocksX-NG/PingClient.swift @@ -94,17 +94,21 @@ class PingServers:NSObject{ } if i >= SerMgr.profiles.count{ - let notice = NSUserNotification() - notice.title = "Ping测试完成!" - notice.subtitle = "最快的是\(SerMgr.profiles[fastest_id].remark) \(SerMgr.profiles[fastest_id].serverHost) \(SerMgr.profiles[fastest_id].latency!)ms" - NSUserNotificationCenter.default.deliver(notice) + DispatchQueue.main.async { + // do the UI update HERE + let notice = NSUserNotification() + notice.title = "Ping测试完成!" + notice.subtitle = "最快的是\(self.SerMgr.profiles[self.fastest_id].remark) \(self.SerMgr.profiles[self.fastest_id].serverHost) \(self.SerMgr.profiles[self.fastest_id].latency!)ms" + NSUserNotificationCenter.default.deliver(notice) + } return } - let host = SerMgr.profiles[i].serverHost + let host = self.SerMgr.profiles[i].serverHost SimplePingClient.pingHostname(host) { latency in - print("-----------\(host) latency is \(latency ?? "fail")") + DispatchQueue.global().async { + print("[Ping Result]-\(host) latency is \(latency ?? "fail")") self.SerMgr.profiles[i].latency = latency ?? "fail" - + if latency != nil { if self.fastest == nil{ self.fastest = latency @@ -115,10 +119,13 @@ class PingServers:NSObject{ self.fastest_id = i } } - (NSApplication.shared().delegate as! AppDelegate).updateServersMenu() - (NSApplication.shared().delegate as! AppDelegate).updateRunningModeMenu() + DispatchQueue.main.async { + // do the UI update HERE + (NSApplication.shared().delegate as! AppDelegate).updateServersMenu() + (NSApplication.shared().delegate as! AppDelegate).updateRunningModeMenu() + } + } } - self.ping(i+1) } }