Commit 83d12b23 authored by Morten Nielsen's avatar Morten Nielsen Committed by Jason Williams
Browse files

Refactored use of async. (#215)

* Refactored use of async. No tight while-loops but instead using await and continue on background threads.

* Remove use of .Wait() to avoid potential deadlocks. increase use of ConfigureAwait, and remove await where not needed

* Simplified async code. Instead of forwarding to tasks and have to switch to ui thread, it's easier to just await and continue on the same thread here.
parent 5870eb3e
Loading
Loading
Loading
Loading
+146 −259
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ namespace SampleWdpClient.UniversalWindows
        /// </summary>
        /// <param name="sender">The caller of this method.</param>
        /// <param name="e">The arguments associated with this event.</param>
        private void ConnectToDevice_Click(object sender, RoutedEventArgs e)
        private async void ConnectToDevice_Click(object sender, RoutedEventArgs e)
        {
            this.EnableConnectionControls(false);
            this.EnableDeviceControls(false);
@@ -78,12 +78,10 @@ namespace SampleWdpClient.UniversalWindows
                    this.password.Password));

            StringBuilder sb = new StringBuilder();
            Task connectTask = new Task(
                async () =>
                {
                    sb.Append(this.MarshalGetCommandOutput());

            sb.Append(this.commandOutput.Text);
            sb.AppendLine("Connecting...");
                    this.MarshalUpdateCommandOutput(sb.ToString());
            this.commandOutput.Text = sb.ToString();
            portal.ConnectionStatus += (portal, connectArgs) =>
            {
                if (connectArgs.Status == DeviceConnectionStatus.Connected)
@@ -122,17 +120,9 @@ namespace SampleWdpClient.UniversalWindows
                sb.AppendLine(exception.Message);
            }

                    this.MarshalUpdateCommandOutput(sb.ToString());
                });

            Task continuationTask = connectTask.ContinueWith(
                (t) =>
                {
                    this.MarshalEnableDeviceControls(true);
                    this.MarshalEnableConnectionControls(true);
                });

            connectTask.Start();
            this.commandOutput.Text = sb.ToString();
            EnableDeviceControls(true);
            EnableConnectionControls(true);
        }

        /// <summary>
@@ -179,19 +169,16 @@ namespace SampleWdpClient.UniversalWindows
        /// </summary>
        /// <param name="sender">The caller of this method.</param>
        /// <param name="e">The arguments associated with this event.</param>
        private void GetIPConfig_Click(object sender, RoutedEventArgs e)
        private async void GetIPConfig_Click(object sender, RoutedEventArgs e)
        {
            this.ClearOutput();
            this.EnableConnectionControls(false);
            this.EnableDeviceControls(false);

            StringBuilder sb = new StringBuilder();
            Task getTask = new Task(
                async () =>
                {
                    sb.Append(this.MarshalGetCommandOutput());
            sb.Append(commandOutput.Text);
            sb.AppendLine("Getting IP configuration...");
                    this.MarshalUpdateCommandOutput(sb.ToString());
            commandOutput.Text = sb.ToString();

            try
            {
@@ -217,17 +204,10 @@ namespace SampleWdpClient.UniversalWindows
                sb.AppendLine("Failed to get IP config info.");
                sb.AppendLine(ex.GetType().ToString() + " - " + ex.Message);
            }
                });

            Task continuationTask = getTask.ContinueWith(
                (t) =>
                {
                    this.MarshalUpdateCommandOutput(sb.ToString());
                    this.MarshalEnableDeviceControls(true);
                    this.MarshalEnableConnectionControls(true);
                });

            getTask.Start();
            commandOutput.Text = sb.ToString();
            EnableDeviceControls(true);
            EnableConnectionControls(true);
        }

        /// <summary>
@@ -235,19 +215,17 @@ namespace SampleWdpClient.UniversalWindows
        /// </summary>
        /// <param name="sender">The caller of this method.</param>
        /// <param name="e">The arguments associated with this event.</param>
        private void GetWifiInfo_Click(object sender, RoutedEventArgs e)
        private async void GetWifiInfo_Click(object sender, RoutedEventArgs e)
        {
            this.ClearOutput();
            this.EnableConnectionControls(false);
            this.EnableDeviceControls(false);

            StringBuilder sb = new StringBuilder();
            Task getTask = new Task(
                async () =>
                {
                    sb.Append(this.MarshalGetCommandOutput());

            sb.Append(commandOutput.Text);
            sb.AppendLine("Getting WiFi interfaces and networks...");
                    this.MarshalUpdateCommandOutput(sb.ToString());
            commandOutput.Text = sb.ToString();

            try
            {
@@ -284,81 +262,10 @@ namespace SampleWdpClient.UniversalWindows
                sb.AppendLine("Failed to get WiFi info.");
                sb.AppendLine(ex.GetType().ToString() + " - " + ex.Message);
            }
                });

            Task continuationTask = getTask.ContinueWith(
                (t) =>
                {
                    this.MarshalUpdateCommandOutput(sb.ToString());
                    this.MarshalEnableDeviceControls(true);
                    this.MarshalEnableConnectionControls(true);
                });

            getTask.Start();
        }

        /// <summary>
        /// Executes the EnabledConnectionControls method on the UI thread.
        /// </summary>
        /// <param name="enable">True to enable the controls, false to disable them.</param>
        private void MarshalEnableConnectionControls(bool enable)
        {
            Task t = this.Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                () =>
                {
                    this.EnableConnectionControls(enable);
                }).AsTask();
            t.Wait();
        }

        /// <summary>
        /// Executes the EnabledDeviceControls method on the UI thread.
        /// </summary>
        /// <param name="enable">True to enable the controls, false to disable them.</param>
        private void MarshalEnableDeviceControls(bool enable)
        {
            Task t = this.Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                () =>
                {
                    this.EnableDeviceControls(enable);
                }).AsTask();
            t.Wait();
        }

        /// <summary>
        /// Executes the fetching of the text displayed in the command output UI element on the UI thread.
        /// </summary>
        /// <returns>The contents of the command output UI element.</returns>
        private string MarshalGetCommandOutput()
        {
            string output = string.Empty;

            Task t = this.Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                () =>
                {
                    output = this.commandOutput.Text;
                }).AsTask();
            t.Wait();

            return output;
        }

        /// <summary>
        /// Executes the update of the text displayed in the command output UI element ont he UI thread.
        /// </summary>
        /// <param name="output">The text to display in the command output UI element.</param>
        private void MarshalUpdateCommandOutput(string output)
        {
            Task t = this.Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                () =>
                {
                    this.commandOutput.Text = output;
                }).AsTask();
            t.Wait();
            commandOutput.Text = sb.ToString();
            EnableDeviceControls(true);
            EnableConnectionControls(true);
        }

        /// <summary>
@@ -376,7 +283,7 @@ namespace SampleWdpClient.UniversalWindows
        /// </summary>
        /// <param name="sender">The caller of this method.</param>
        /// <param name="e">The arguments associated with this event.</param>
        private void RebootDevice_Click(object sender, RoutedEventArgs e)
        private async void RebootDevice_Click(object sender, RoutedEventArgs e)
        {
            bool reenableDeviceControls = false;

@@ -385,12 +292,10 @@ namespace SampleWdpClient.UniversalWindows
            this.EnableDeviceControls(false);

            StringBuilder sb = new StringBuilder();
            Task rebootTask = new Task(
                async () =>
                {
                    sb.Append(this.MarshalGetCommandOutput());

            sb.Append(commandOutput.Text);
            sb.AppendLine("Rebooting the device");
                    this.MarshalUpdateCommandOutput(sb.ToString());
            commandOutput.Text = sb.ToString();

            try
            {
@@ -402,17 +307,10 @@ namespace SampleWdpClient.UniversalWindows
                sb.AppendLine(ex.GetType().ToString() + " - " + ex.Message);
                reenableDeviceControls = true;
            }
                });

            Task continuationTask = rebootTask.ContinueWith(
                (t) =>
                {
                    this.MarshalUpdateCommandOutput(sb.ToString());
                    this.MarshalEnableDeviceControls(reenableDeviceControls);
                    this.MarshalEnableConnectionControls(true);
                });

            rebootTask.Start();
            commandOutput.Text = sb.ToString();
            EnableDeviceControls(reenableDeviceControls);
            EnableConnectionControls(true);
        }

        /// <summary>
@@ -420,7 +318,7 @@ namespace SampleWdpClient.UniversalWindows
        /// </summary>
        /// <param name="sender">The caller of this method.</param>
        /// <param name="e">The arguments associated with this event.</param>
        private void ShutdownDevice_Click(object sender, RoutedEventArgs e)
        private async void ShutdownDevice_Click(object sender, RoutedEventArgs e)
        {
            bool reenableDeviceControls = false;

@@ -429,13 +327,9 @@ namespace SampleWdpClient.UniversalWindows
            this.EnableDeviceControls(false);

            StringBuilder sb = new StringBuilder();
            Task shutdownTask = new Task(
                async () =>
                {
                    sb.Append(this.MarshalGetCommandOutput());
            sb.Append(commandOutput.Text);
            sb.AppendLine("Shutting down the device");
                    this.MarshalUpdateCommandOutput(sb.ToString());

            commandOutput.Text = sb.ToString();
            try
            {
                await portal.ShutdownAsync();
@@ -446,17 +340,10 @@ namespace SampleWdpClient.UniversalWindows
                sb.AppendLine(ex.GetType().ToString() + " - " + ex.Message);
                reenableDeviceControls = true;
            }
                });

            Task continuationTask = shutdownTask.ContinueWith(
                (t) =>
                {
                    this.MarshalUpdateCommandOutput(sb.ToString());
                    this.MarshalEnableDeviceControls(reenableDeviceControls);
                    this.MarshalEnableConnectionControls(true);
                });

            shutdownTask.Start();
            commandOutput.Text = sb.ToString();
            EnableDeviceControls(reenableDeviceControls);
            EnableConnectionControls(true);
        }

        /// <summary>
+1 −1
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new DevicePortalException(response);
                        throw await DevicePortalException.CreateAsync(response);
                    }

                    if (response.Content != null)
+1 −1
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new DevicePortalException(response);
                        throw await DevicePortalException.CreateAsync(response);
                    }

                    using (HttpContent content = response.Content)
+1 −1
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new DevicePortalException(response);
                        throw await DevicePortalException.CreateAsync(response);
                    }

                    if (response.Content != null)
+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ namespace Microsoft.Tools.WindowsDevicePortal
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new DevicePortalException(response);
                        throw await DevicePortalException.CreateAsync(response);
                    }

                    if (response.Content != null)
Loading