How to make sure that all the threads started by the test(s) are stopped before completion
<p>I am building a windows service that communicates with other processes using <a href="https://github.com/acdvorak/named-pipe-wrapper" rel="noreferrer">named pipe</a>.
My unit test for the named pipe communication is throwing this error message 4 times:</p>

<blockquote>
  <p>System.AppDomainUnloadedException: Attempted to access an unloaded
  AppDomain. This can happen if the test(s) started a thread but did not
  stop it. Make sure that all the threads started by the test(s) are
  stopped before completion.</p>
</blockquote>

<p>Here's my unit test:</p>

<pre><code>    [TestMethod]
    public void ListenToNamedPipeTest()
    {
        var watcher = new ManualResetEvent(false);

        var svc = new WindowService();
        svc.ClientMessageHandler += (connection, message) =&gt; watcher.Reset();
        svc.ListenToNamedPipe();
        sendMessageToNamedPipe("bla");
        var wait = watcher.WaitOne(1000);

        svc.Dispose();

        Assert.IsTrue(wait, "No messages received after 1 seconds");
    }

    private void sendMessageToNamedPipe(string text)
    {
        var client = new NamedPipeClient&lt;Message, Message&gt;(DeviceCertificateService.PIPE_NAME);
        client.ServerMessage += (conn, message) =&gt; Console.WriteLine("Server says: {0}", message.Text);

        // Start up the client asynchronously and connect to the specified server pipe.
        // This method will return immediately while the client runs in a separate background thread.
        client.Start();

        client.PushMessage(new Message { Text = text });

        client.Stop();
    }
</code></pre>

<p>How do I make all threads stop before my unit test stops?</p>

<p>Thanks</p>

<hr>

<p>UPDATE:</p>

<p>The named pipe client does not have a <code>close()</code> function:</p>

<pre><code>// Type: NamedPipeWrapper.NamedPipeClient`2
// Assembly: NamedPipeWrapper, Version=1.5.0.0, Culture=neutral, PublicKeyToken=null
// MVID: D2B99F4D-8C17-4DB6-8A02-29DCF82A4118
// Assembly location: C:\Users\Thang.Duong\Source\Workspaces\Post Tracking System\Applications\Dev\OHD\packages\NamedPipeWrapper.1.5.0\lib\net40\NamedPipeWrapper.dll

using System;

namespace NamedPipeWrapper
{
  public class NamedPipeClient&lt;TRead, TWrite&gt; where TRead : class where TWrite : class
  {
    public NamedPipeClient(string pipeName);
    public void Start();
    public void PushMessage(TWrite message);
    public void Stop();
    public void WaitForConnection();
    public void WaitForConnection(int millisecondsTimeout);
    public void WaitForConnection(TimeSpan timeout);
    public void WaitForDisconnection();
    public void WaitForDisconnection(int millisecondsTimeout);
    public void WaitForDisconnection(TimeSpan timeout);
    public bool AutoReconnect { get; set; }
    public event ConnectionMessageEventHandler&lt;TRead, TWrite&gt; ServerMessage;
    public event ConnectionEventHandler&lt;TRead, TWrite&gt; Disconnected;
    public event PipeExceptionEventHandler Error;
  }
}
</code></pre>