using HslCommunication.Core; using HslCommunication.LogNet; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HslCommunication.Enthernet { /// /// 文件标记对象类 /// internal class FileMarkId { /// /// 实例化一个文件标记对象 /// /// 日志对象 /// 完整的文件名称 public FileMarkId( ILogNet logNet, string fileName ) { LogNet = logNet; FileName = fileName; } private ILogNet LogNet; // 日志 private string FileName = null; // 文件名称 private Queue queues = new Queue( ); // 操作的队列 private SimpleHybirdLock hybirdLock = new SimpleHybirdLock( ); // 状态的锁 /// /// 新增一个文件的操作,仅仅是删除文件 /// /// 对当前文件的操作内容 public void AddOperation( Action action ) { hybirdLock.Enter( ); if (readStatus == 0) { // 没有读取状态,立马执行 action?.Invoke( ); } else { // 添加标记 queues.Enqueue( action ); } hybirdLock.Leave( ); } private int readStatus = 0; /// /// 指示该对象是否能被清除 /// /// 是否能够删除 public bool CanClear( ) { bool result = false; hybirdLock.Enter( ); result = readStatus == 0 && queues.Count == 0; hybirdLock.Leave( ); return result; } /// /// 进入文件的读取状态 /// public void EnterReadOperator( ) { hybirdLock.Enter( ); readStatus++; hybirdLock.Leave( ); } /// /// 离开本次的文件读取状态 /// public void LeaveReadOperator( ) { // 检查文件标记状态 hybirdLock.Enter( ); readStatus--; if (readStatus == 0) { while (queues.Count > 0) { try { queues.Dequeue( )?.Invoke( ); } catch (Exception ex) { LogNet?.WriteException( "FileMarkId", "File Action Failed:", ex ); } } } hybirdLock.Leave( ); } } }