//------------------------------------------------------------------------------------------------------------------------
public void Stop(bool CloseAllChannels = false)
{
try
{
lock (this)
{
//close all channels
if (CloseAllChannels)
{
_Channels.ForEach(c => { try { c.Close("Server stopped"); } catch (Exception ex) { DebugEx.Assert(ex, "Error while closing channel"); } });
_Channels.Clear();
}
//update flag
if (!_IsRunning)
{
return;
}
else
{
_IsRunning = false;
}
//close my socket
try
{
if (sock != null)
{
#if NETFX
try { sock.Close(); } catch { }
#endif
try { sock.Dispose(); } catch { }
}
}
catch (Exception ex)
{
DebugEx.TraceErrorException(ex);
}
sock = null;
//wait for task finish
#if NETFX
PortListener?.Join(1000);
#elif UNIVERSAL
PortListener?.Wait(1000);
#endif
PortListener = null;
}
}
catch (Exception ex)
{
DebugEx.Assert(ex, "YPChannel Server Stop() failed");
}
}