This is work (_stream and _listener are private fields, initialising into task):
public void Stop()
{
if (_listenTask != null)
{
_source.Cancel();
try
{
_stream?.Close();
}
catch (Exception)
{ }
try
{
_listener?.Stop();
}
catch (Exception)
{ }
while (!(_listenTask.IsCanceled || _listenTask.IsCompleted || _listenTask.IsFaulted))
{
Thread.Sleep(1);
}
_listenTask = null;
_source = null;
}
}