In a WPF application, I use TcpClient to connect to a remote server. The application has a tree; when a node is selected, the code is executed:
public partial class MainWindow : Window, INotifyPropertyChanged { ... TreeView se_TreeView; // предполагаем, что в XAML ... // Выбор узла дерева private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { ... (se_TreeView.SelectedItem as TCPConnection)._tmpClient = new TcpClient(); try { (se_TreeView.SelectedItem as TCPConnection)._tmpClient.BeginConnect(IP, Port, new AsyncCallback(ConnectCallback), tmpObj); } catch (Exception ex) { // вывод нужной инф в файл. } } // Закрытие приложения private void Window_Closing(object sender, CancelEventArgs e) { ... if (se_TreeView.SelectedItem != null) { if ((se_TreeView.SelectedItem as TCPConnection)._tmpStream != null) ((se_TreeView.SelectedItem as TCPConnection)._tmpStream.Close(); if ((se_TreeView.SelectedItem as TCPConnection)._tmpClient != null) ((se_TreeView.SelectedItem as TCPConnection)._tmpClient.Close(); } ... } } There is also a class that represents the contents of the tree node:
public class TCPConnection { ... public TcpClient _tmpClient; public NetworkStream _tmpStream; ... } In general, each time you start an application and select a tree item, BeginConnect () throws an exception:
ObjectDisposedException Access to a liquidated object is not possible. Object name: "System.Net.Sockets.Socket". System in System.Net.Sockets.Socket.BeginConnect (IPAddress address, Int32 port, AsyncCallback requestCallback, Object state) in System.Net.Sockets.TcpClient.BeginConnect (IPAddress address, Int32 port, AsyncCallback requestCallback, Object state) in ACCCtrl. MainWindow.CS1606CheckConnect (TreeViewLocation tvl, Int32 index) in C: \ Users \ Documents \ CS1606 Asynch \ ACCCtrl \ MainWindow.xaml.cs: line 9752
Repeats stably, the client is created without errors, when closing the application there are no errors. But I noticed that when _tmpClient is created, then _tmpClient.Client.Blocking is true
_tmpClient.Client.Blockingshould be true, look for an error elsewhere - Pavel Mayorov