Ejemplo n.º 1
0
        /// <summary>
        /// Click handler for the shutdownDevice button.
        /// </summary>
        /// <param name="sender">The caller of this method.</param>
        /// <param name="e">The arguments associated with this event.</param>
        private async void ShutdownDevice_Click(object sender, RoutedEventArgs e)
        {
            bool reenableDeviceControls = false;

            this.ClearOutput();
            this.EnableConnectionControls(false);
            this.EnableDeviceControls(false);

            StringBuilder sb = new StringBuilder();

            sb.Append(commandOutput.Text);
            sb.AppendLine("Shutting down the device");
            commandOutput.Text = sb.ToString();
            try
            {
                await portal.ShutdownAsync();
            }
            catch (Exception ex)
            {
                sb.AppendLine("Failed to shut down the device.");
                sb.AppendLine(ex.GetType().ToString() + " - " + ex.Message);
                reenableDeviceControls = true;
            }

            commandOutput.Text = sb.ToString();
            EnableDeviceControls(reenableDeviceControls);
            EnableConnectionControls(true);
        }
        /// <summary>
        /// Click handler for the shutdownDevice button.
        /// </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)
        {
            bool reenableDeviceControls = false;

            this.ClearOutput();
            this.EnableConnectionControls(false);
            this.EnableDeviceControls(false);

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

                try
                {
                    await portal.ShutdownAsync();
                }
                catch (Exception ex)
                {
                    sb.AppendLine("Failed to shut down the device.");
                    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();
        }