public object CallMain(string name, Package data, bool throwOnError = true) { if (name == null) { return(null); } //获取同步状态 if (!worker.AcquireSyncing()) { if (!throwOnError) { return(null); } throw new Exception("Other thread is syncing!"); } //写入主线程 locker.AcquireWriterLock(lockTimeout); this.m_eventName = name; this.m_eventData = data; locker.ReleaseWriterLock(); //等待主线程同步 try { while (worker.IsAlive) { locker.AcquireReaderLock(lockTimeout); if (this.m_eventName == null) { break; } locker.ReleaseReaderLock(); } return(this.m_eventData); } finally { if (locker.IsReaderLockHeld) { locker.ReleaseReaderLock(); } worker.ReleaseSyncing(); } }
public bool FileExists(string filepath) { bool result = false; if (this.state.TryGetValue(filepath, out result)) { return(result); } //获取同步状态 if (!worker.AcquireSyncing()) { throw new Exception("Other thread is syncing!"); } //写入主线程 locker.AcquireWriterLock(lockTimeout); this.filePath = filepath; this.fileExists = false; locker.ReleaseWriterLock(); //等待主线程同步 try { while (worker.IsAlive) { locker.AcquireReaderLock(lockTimeout); if (this.filePath == null) { break; } locker.ReleaseReaderLock(); } this.state.Add(filepath, this.fileExists); return(this.fileExists); } finally { if (locker.IsReaderLockHeld) { locker.ReleaseReaderLock(); } worker.ReleaseSyncing(); } }