Ejemplo n.º 1
0
        public bool Stop(int millisecondsTimeout = 0)
        {
            bool stopConfirmed = false;

            try {
                lock (this) {
                    // state checks
                    if (this.IsListening == false)
                    {
                        // already stopped
                        return(true);
                    }
                    Debug.Assert(this.IsDisposed == false);

                    // log
                    bool verbose = ShouldLog(TraceEventType.Verbose);
                    if (verbose)
                    {
                        LogVerbose("Stopping...");
                    }

                    // stop listening
                    IReadOnlyList <Listener> listeners = this.listeners;
                    Debug.Assert(listeners != null);
                    Parallel.ForEach(
                        listeners,
                        (listener) => {
                        try {
                            listener.Stop();
                        } catch {
                            // continue
                        }
                    }
                        );

                    // stop connections
                    ConnectionCollection connections = this.connections;
                    this.connections = null;
                    if (connections != null)
                    {
                        connections.StopAll();
                    }

                    // wait for the completion of the tasks
                    // Note that -1 timeout means 'Infinite'.
                    if (millisecondsTimeout != 0)
                    {
                        List <Task> tasks = new List <Task>();
                        tasks.AddRange(TaskingComponent.GetActiveTaskList(listeners));
                        if (connections != null)
                        {
                            tasks.AddRange(connections.GetActiveTaskList());
                        }

                        if (0 < tasks.Count)
                        {
                            stopConfirmed = Task.WaitAll(tasks.ToArray(), millisecondsTimeout);
                        }
                        else
                        {
                            stopConfirmed = true;
                        }
                    }

                    // update its state
                    this.Runner = null;
                    if (stopConfirmed)
                    {
                        if (verbose)
                        {
                            LogVerbose("Stopped.");
                        }
                    }
                    else
                    {
                        string message = "Requested to stop, but did not comfirm actual stop.";
                        if (millisecondsTimeout == 0)
                        {
                            LogVerbose(message);
                        }
                        else
                        {
                            LogWarning(message);
                        }
                    }
                }
            } catch (Exception exception) {
                LogError($"Fail to stop: {exception.Message}");
                throw;
            }

            return(stopConfirmed);
        }
Ejemplo n.º 2
0
 public Task[] GetActiveTaskList()
 {
     lock (this) {
         return(TaskingComponent.GetActiveTaskList(this.connectionList));
     }
 }