From 926cfbf3bc19809c1d97c79161aaa9ee6e491886 Mon Sep 17 00:00:00 2001 From: Mitchell Sternke Date: Thu, 6 Oct 2016 10:15:59 -0700 Subject: [PATCH] Fixed issue where connecting could take very long and cancellation would not work (#78) * Fixed issue where connecting could take very long and cancellation would not work * Addressing feedback * Remove warning suppression --- .../Connection/ConnectionService.cs | 55 +++++++++++-------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs index fd06343c..36e86791 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Connection/ConnectionService.cs @@ -264,10 +264,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection }; // invoke callback notifications - foreach (var activity in this.onConnectionActivities) - { - await activity(connectionInfo); - } + invokeOnConnectionActivities(connectionInfo); // try to get information about the connected SQL Server instance try @@ -461,24 +458,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection try { - // create a task to connect asyncronously so that other requests are not blocked in the meantime - Task.Run(async () => - { - try - { - // open connection based on request details - ConnectionCompleteParams result = await ConnectionService.Instance.Connect(connectParams); - await ServiceHost.SendEvent(ConnectionCompleteNotification.Type, result); - } - catch (Exception ex) - { - ConnectionCompleteParams result = new ConnectionCompleteParams() - { - Messages = ex.ToString() - }; - await ServiceHost.SendEvent(ConnectionCompleteNotification.Type, result); - } - }); + RunConnectRequestHandlerTask(connectParams, requestContext); await requestContext.SendResult(true); } catch @@ -487,6 +467,28 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection } } + private void RunConnectRequestHandlerTask(ConnectParams connectParams, RequestContext requestContext) + { + // create a task to connect asynchronously so that other requests are not blocked in the meantime + Task.Run(async () => + { + try + { + // open connection based on request details + ConnectionCompleteParams result = await ConnectionService.Instance.Connect(connectParams); + await ServiceHost.SendEvent(ConnectionCompleteNotification.Type, result); + } + catch (Exception ex) + { + ConnectionCompleteParams result = new ConnectionCompleteParams() + { + Messages = ex.ToString() + }; + await ServiceHost.SendEvent(ConnectionCompleteNotification.Type, result); + } + }); + } + /// /// Handle cancel connect requests /// @@ -723,5 +725,14 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection } } } + + private void invokeOnConnectionActivities(ConnectionInfo connectionInfo) + { + foreach (var activity in this.onConnectionActivities) + { + // not awaiting here to allow handlers to run in the background + activity(connectionInfo); + } + } } }