StopHostedService() private method

private StopHostedService ( ) : void
return void
示例#1
0
        /// <summary>
        /// Runs the console host.
        /// </summary>
        public void Run()
        {
            if ((object)m_serviceHost == null)
            {
                throw new NullReferenceException("Service host reference is null");
            }

            string userInput = "";

            try
            {
                Thread.CurrentPrincipal      = new GenericPrincipal(new GenericIdentity(UserInfo.CurrentUserID), new[] { "Administrator" });
                m_serviceHost.UpdatedStatus += m_serviceHost_UpdatedStatus;
                m_serviceHost.StartHostedService();
                m_serviceHost.SendRequest(m_clientID, "Filter -Remove 0");

                while (!string.Equals(userInput, "Exit", StringComparison.OrdinalIgnoreCase))
                {
                    // Wait for a command from the user.
                    userInput = System.Console.ReadLine();

                    // Write a blank line to the console.
                    WriteLine();

                    if (!string.IsNullOrWhiteSpace(userInput))
                    {
                        // The user typed in a command and didn't just hit <ENTER>.
                        switch (userInput.ToUpper())
                        {
                        case "CLS":
                            // User wants to clear the console window.
                            System.Console.Clear();
                            break;

                        case "EXIT":
                            // User wants to exit hosted session.
                            break;

                        default:
                            // User wants to send a request to the service.
                            m_serviceHost.SendRequest(m_clientID, userInput);

                            if (string.Compare(userInput, "Help", StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                DisplayHelp();
                            }

                            break;
                        }
                    }
                }
            }
            finally
            {
                m_serviceHost.StopHostedService();
                m_serviceHost.UpdatedStatus -= m_serviceHost_UpdatedStatus;
            }
        }
示例#2
0
        private void DebugHost_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!DesignMode)
            {
                if (MessageBox.Show($"Are you sure you want to stop {m_productName} service? ",
                                    "Stop Service", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    // Stop the windows service.
                    ServiceHost.StopHostedService();

                    // Call user overridable debug host unloading method
                    DebugHostUnloading();
                }
                else
                {
                    // Stop the application from exiting.
                    e.Cancel = true;
                }
            }
        }