コード例 #1
0
        private bool EnableDisableDevice(bool enabled)
        {
            while (true)
            {
                try
                {
                    // Establish a new connection to the Windows service if there isn't one available to complete the action
                    if (TTService == null)
                    {
                        if (InitialiseServiceConnection())
                        {
                            continue; // retry
                        }
                        else
                        {
                            return(false); // failed, abort action
                        }
                    }

                    // Perform the enable/disable action
                    string errorMsg = TTService.EnableDevice(enabled);

                    // If not success
                    if (errorMsg != "")
                    {
                        string userMessage = "Unknown error";

                        if (errorMsg.Contains("No device found matching filter"))
                        {
                            userMessage = "Device not found";
                        }

                        // Show the user a message
                        MessageBox.Show(userMessage, "Touch Toggler Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return(false); // failed to make device change
                    }

                    // Success
                    return(true);
                }
                catch (CommunicationException)
                {
                    if (!ServiceCommunicationFailure())
                    {
                        return(false);
                    }
                }
            }
        }