Beispiel #1
0
        public void Stop()
        {
            if (server != null)
            {
                Console.WriteLine("Server stopped. Waiting for exit...");

                using (SampleServer _server = server)
                {
                    // Stop status thread
                    server = null;
                    if (status != null)
                    {
                        status.Wait();
                    }
                    // Stop server and dispose
                    if (_server != null)
                    {
                        _server.Stop();
                    }

                    FinallyStopped = AllowFinallyStopped;

                    Log.Info("End of Server stopping!");
                }
            }
        }
        public void Run()
        {
            try
            {
                ExitCode = ExitCode.ErrorServerNotStarted;
                ConsoleSampleServer().Wait();
                Console.WriteLine("Server started. Press Ctrl-C to exit...");
                ExitCode = ExitCode.ErrorServerRunning;
            }
            catch (Exception ex)
            {
                Utils.Trace("ServiceResultException:" + ex.Message);
                Console.WriteLine("Exception: {0}", ex.Message);
                ExitCode = ExitCode.ErrorServerException;
                return;
            }

            ManualResetEvent quitEvent = new ManualResetEvent(false);

            try
            {
                Console.CancelKeyPress += (sender, eArgs) => {
                    quitEvent.Set();
                    eArgs.Cancel = true;
                };
            }
            catch
            {
            }

            // wait for timeout or Ctrl-C
            quitEvent.WaitOne(ServerRunTime);

            if (Server != null)
            {
                Console.WriteLine("Server stopped. Waiting for exit...");

                using (SampleServer _server = Server)
                {
                    Server.CurrentInstance.SessionManager.SessionActivated -= EventStatus;
                    Server.CurrentInstance.SessionManager.SessionClosing   -= EventStatus;
                    Server.CurrentInstance.SessionManager.SessionCreated   -= EventStatus;

                    // Stop status thread
                    Server = null;
                    Status.Wait();
                    // Stop server and dispose
                    _server.Stop();
                }
            }

            ExitCode = ExitCode.Ok;
        }
        public void Run()
        {
            try
            {
                ConsoleSampleServer().Wait();
                Console.WriteLine("Server started. Press any key to exit...");
            }
            catch (Exception ex)
            {
                Utils.Trace("ServiceResultException:" + ex.Message);
                Console.WriteLine("Exception: {0}", ex.Message);
            }

            try
            {
                Console.ReadKey(true);
            }
            catch
            {
                // wait forever if there is no console
                Thread.Sleep(Timeout.Infinite);
            }

            if (server != null)
            {
                Console.WriteLine("Server stopped. Waiting for exit...");

                using (SampleServer _server = server)
                {
                    // Stop status thread
                    server = null;
                    status.Wait();
                    // Stop server and dispose
                    _server.Stop();
                }
            }
        }