From 12c2cc4e22e123c7859caff8add6ce2df505495b Mon Sep 17 00:00:00 2001 From: maqiao <625215135@qq.com> Date: Sat, 18 Jan 2025 15:08:04 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E5=87=BA=E8=BD=AF=E4=BB=B6=E6=96=AD?= =?UTF-8?q?=E5=BC=80=E4=B8=8E=E6=9D=BF=E5=AD=90=E7=9A=84=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DM_Weight/Port/SocketHelper.cs | 50 ++++++++++++++------ DM_Weight/ViewModels/LoginWindowViewModel.cs | 1 + 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/DM_Weight/Port/SocketHelper.cs b/DM_Weight/Port/SocketHelper.cs index 2a77607..908a8a7 100644 --- a/DM_Weight/Port/SocketHelper.cs +++ b/DM_Weight/Port/SocketHelper.cs @@ -6,6 +6,7 @@ using DM_Weight.ViewModels; using DM_Weight.Views; using log4net; using Mina.Core.Future; +using Mina.Core.Session; using Mina.Filter.Codec; using Mina.Filter.Logging; using Mina.Transport.Socket; @@ -27,12 +28,12 @@ namespace DM_Weight.Port private readonly ILog logger = LogManager.GetLogger(typeof(SocketHelper)); AsyncSocketConnector acceptor = new AsyncSocketConnector(); IConnectFuture iConnectFuture; - public bool OpenStatus=false; + public bool OpenStatus = false; public bool ConnectedStatus = false; //多线程退出标识 public bool IsMultiThread = false; string ip = ConfigurationManager.AppSettings["modbusIp"].ToString(); - int port =Convert.ToInt32(ConfigurationManager.AppSettings["modbusPort"]); + int port = Convert.ToInt32(ConfigurationManager.AppSettings["modbusPort"]); IEventAggregator _eventAggregator; public DateTime dateTime { get; set; } = DateTime.Now; FingerprintUtil _fingerprintUtil; @@ -70,16 +71,17 @@ namespace DM_Weight.Port //} }; - acceptor.SessionClosed += (o, e) => - { - logger.Info("SessionClosed"); - //Task.Delay(50).Wait(); - SocketConnect(); - Task.Factory.StartNew(() => - { - _fingerprintUtil.FingerDisconnect(); - }); - }; + acceptor.SessionClosed += new EventHandler(SessionCloseMethod); + //acceptor.SessionClosed += (o, e) => + //{ + // logger.Info("SessionClosed"); + // //Task.Delay(50).Wait(); + // SocketConnect(); + // Task.Factory.StartNew(() => + // { + // _fingerprintUtil.FingerDisconnect(); + // }); + //}; this.SocketConnect(); _fingerprintUtil = fingerprintUtil; } @@ -94,7 +96,7 @@ namespace DM_Weight.Port { if (i <= 3) { - Thread.Sleep(1950); + Thread.Sleep(50); //没连上会再连两次 SocketConnect(); } @@ -115,7 +117,27 @@ namespace DM_Weight.Port } ConnectedStatus = iConnectFuture.Connected; } - + private void SessionCloseMethod(System.Object o, IoSessionEventArgs e) + { + logger.Info($"SessionClosed"); + SocketConnect(); + Task.Factory.StartNew(() => + { + _fingerprintUtil.FingerDisconnect(); + }); + } + public void SocketDisConnect() + { + try + { + acceptor.SessionClosed -= new EventHandler(SessionCloseMethod); + iConnectFuture.Session.CloseNow(); + } + catch(Exception ex) + { + logger.Info($"SocketDisConnect异常{ex.Message}"); + } + } public void SendMessage(MyBaseMessage baseMessage) { if (!ConnectedStatus) diff --git a/DM_Weight/ViewModels/LoginWindowViewModel.cs b/DM_Weight/ViewModels/LoginWindowViewModel.cs index b8fd421..ce40520 100644 --- a/DM_Weight/ViewModels/LoginWindowViewModel.cs +++ b/DM_Weight/ViewModels/LoginWindowViewModel.cs @@ -374,6 +374,7 @@ _exitCommand ??= new DelegateCommand(Exit); void Exit() { //_chkFunction.HIKLoginOut(); + _socketHelper.SocketDisConnect(); Process.GetCurrentProcess().Kill(); Environment.Exit(0); }