我实现了用作 TcpCLient 服务器的类。看起来像这样:
{
[Export]
public class MessageListener
{
private readonly TcpListener tcpListener;
private readonly Thread listenThread;
private DataRepository DataRepository { get; set; }
private IEventAggregator EventAggregator { get; set; }
[ImportingConstructor]
public MessageListener(DataRepository dataRepository, IEventAggregator eventAggregator)
{
this.DataRepository = dataRepository;
this.EventAggregator = eventAggregator;
// TODO: Need to put proper Port number
this.tcpListener = new TcpListener(IPAddress.Any, 3000);
this.listenThread = new Thread(this.ListenForClients);
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
// blocks until a client has connected to the server
var client = this.tcpListener.AcceptTcpClient();
// create a thread to handle communication with connected client
var clientThread = new Thread(this.HandleClientComm);
clientThread.Start(client);
}
}
这个监听器导入到我的 Shell 视图模型中。效果很好。
当我关闭 WPF 窗口时 - MEF 不会处理此对象。Windows 关闭但进程仍在运行。如何正确关闭此“服务器”?它等待var client
..