void HandleFileWatcherEvent() { if (mLoading || mIODelayTimer.Enabled) { return; } if ((DateTime.Now - mLastLoadTime).TotalMilliseconds > mIODelayTimer.Interval) { LoadRecord(LoadErrorHandlingMode.RetryOnError); } else { mDelayedIOOperation = LoadRecord; mIODelayTimer.Start(); } }
bool LoadRecord(LoadErrorHandlingMode errorHandlingMode) { if (!Visible) { return(true); } if (mDevice == null) { return(false); } mLoading = true; FileStream stream = null; bool watcherRaisesEvents = mDeviceFileWatcher.EnableRaisingEvents; try { var record = (long)mRecordUpDown.Value; if (watcherRaisesEvents) { mDeviceFileWatcher.EnableRaisingEvents = false; } stream = mOpenStream(mDevice.FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); stream.Position = mCalculateBytePosition(record); var readWords = mReadWords(stream, mDevice.RecordWordCount); stream.Close(); mDeviceFileWatcher.EnableRaisingEvents |= watcherRaisesEvents; stream = null; for (int i = 0; i < readWords.Length; i++) { mRecordReadWords[i] = readWords[i]; mRecordEditWords[i].LongValue = readWords[i].LongValue; } ChangesPending = false; mLastReadRecord = record; mLastLoadTime = DateTime.Now; mLoadRecordRetryCount = 0; Update(); } catch (Exception ex) { if (errorHandlingMode == LoadErrorHandlingMode.RetryOnError || (mLoadRecordRetryCount > 0 && mLoadRecordRetryCount <= maxLoadSectorRetryCount)) { mDelayedIOOperation = LoadRecord; mLoadRecordRetryCount++; mIODelayTimer.Start(); return(false); } if (mLoadRecordRetryCount > maxLoadSectorRetryCount) { mDelayedIOOperation = null; mLoadRecordRetryCount = 0; return(false); } if (errorHandlingMode == LoadErrorHandlingMode.ReportAlways || mRecordUpDown.Value != mLastReadRecord) { MessageBox.Show(this, "Unable to load device data: " + ex.Message, "Error loading data", MessageBoxButtons.OK, MessageBoxIcon.Hand); } return(false); } finally { stream?.Close(); mDeviceFileWatcher.EnableRaisingEvents |= watcherRaisesEvents; mLoading = false; } return(true); }
bool LoadRecords(LoadErrorHandlingMode errorHandlingMode) { if (!Visible) { return(true); } if (Device == null) { return(false); } mLoading = true; FileStream stream = null; bool watcherRaisesEvents = mDeviceFileWatcher.EnableRaisingEvents; try { mDeviceFileWatcher.EnableRaisingEvents &= !watcherRaisesEvents; List <IMixByteCollection> readBytes = null; if (File.Exists(Device.FilePath)) { stream = mOpenStream(Device.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); readBytes = mReadDeviceBytes(stream, mDeviceBytesPerRecord); stream.Close(); } else { readBytes = new List <IMixByteCollection>(); } if (readBytes.Count == 0) { readBytes.Add(new MixByteCollection(mDeviceBytesPerRecord)); } mDeviceFileWatcher.EnableRaisingEvents |= watcherRaisesEvents; stream = null; mReadBytes.Clear(); mEditBytes.Clear(); foreach (IMixByteCollection item in readBytes) { mReadBytes.Add(item); mEditBytes.Add((IMixByteCollection)item.Clone()); } SetDeviceRecordCount(mReadBytes.Count); ChangesPending = false; mLastLoadTime = DateTime.Now; mLoadRetryCount = 0; Update(); } catch (Exception ex) { if (errorHandlingMode == LoadErrorHandlingMode.RetryOnError || (mLoadRetryCount > 0 && mLoadRetryCount <= maxLoadRetryCount)) { mDelayedIOOperation = LoadRecords; mLoadRetryCount++; mIODelayTimer.Start(); return(false); } if (mLoadRetryCount > maxLoadRetryCount) { mDelayedIOOperation = null; mLoadRetryCount = 0; return(false); } MessageBox.Show(this, "Unable to load device data: " + ex.Message, "Error loading data", MessageBoxButtons.OK, MessageBoxIcon.Hand); return(false); } finally { stream?.Close(); mDeviceFileWatcher.EnableRaisingEvents |= watcherRaisesEvents; mLoading = false; } return(true); }