diff --git a/Sockets/Samples/SocketsSample.XF/SocketsSample.XF.Droid/SocketsSample.XF.Droid.csproj b/Sockets/Samples/SocketsSample.XF/SocketsSample.XF.Droid/SocketsSample.XF.Droid.csproj index 487ca07..ac4048c 100644 --- a/Sockets/Samples/SocketsSample.XF/SocketsSample.XF.Droid/SocketsSample.XF.Droid.csproj +++ b/Sockets/Samples/SocketsSample.XF/SocketsSample.XF.Droid/SocketsSample.XF.Droid.csproj @@ -20,7 +20,7 @@ - v6.0 + v5.0 true diff --git a/Sockets/Sockets.Implementation.NET/UdpSocketBase.cs b/Sockets/Sockets.Implementation.NET/UdpSocketBase.cs index 3875793..add1965 100644 --- a/Sockets/Sockets.Implementation.NET/UdpSocketBase.cs +++ b/Sockets/Sockets.Implementation.NET/UdpSocketBase.cs @@ -119,6 +119,7 @@ protected Task SendToAsync(byte[] data, int length, string address, int port) .WrapNativeSocketExceptions(); } + /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// @@ -141,7 +142,10 @@ private void Dispose(bool disposing) if (disposing) { if (_backingUdpClient != null) + { ((IDisposable)_backingUdpClient).Dispose(); + _backingUdpClient = null; + } } } diff --git a/Sockets/Sockets.Implementation.NET/UdpSocketClient.cs b/Sockets/Sockets.Implementation.NET/UdpSocketClient.cs index 35ece6f..07b1f37 100644 --- a/Sockets/Sockets.Implementation.NET/UdpSocketClient.cs +++ b/Sockets/Sockets.Implementation.NET/UdpSocketClient.cs @@ -30,6 +30,7 @@ public UdpSocketClient() _backingUdpClient = new UdpClient { EnableBroadcast = true + }; } catch (PlatformSocketException ex) @@ -48,9 +49,11 @@ public Task ConnectAsync(string address, int port) { _messageCanceller = new CancellationTokenSource(); - return Task - .Run(() => this._backingUdpClient.Connect(address, port)) - .WrapNativeSocketExceptions(); + return Task.Run(() => { + _backingUdpClient.Connect(address, port); + base.RunMessageReceiver(_messageCanceller.Token); + }) + .WrapNativeSocketExceptions(); } /// @@ -59,10 +62,17 @@ public Task ConnectAsync(string address, int port) /// public Task DisconnectAsync() { - return Task.Run(() => - { - _messageCanceller.Cancel(); - _backingUdpClient.Close(); + return Task.Run(() => { + if (_messageCanceller != null) + { + _messageCanceller.Cancel(); + _messageCanceller.Dispose(); + _messageCanceller = null; + } + if (_backingUdpClient != null) + { + _backingUdpClient.Close(); + } }); } @@ -110,5 +120,16 @@ public Task DisconnectAsync() { return base.SendToAsync(data, length, address, port); } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public override void Dispose() + { + if (_messageCanceller != null && !_messageCanceller.IsCancellationRequested) + _messageCanceller.Cancel(); + + base.Dispose(); + } } -} \ No newline at end of file +}