void timer_Tick(object sender, EventArgs e) { //this.BeginInvoke((MethodInvoker)(delegate (){ List <Measurement> validMeasurements = new List <Measurement>(); int measurementStartPosition; int measurementEndPosition = 0; bool endOfMeasurementFound = false; if (stopPressed) { return; // Do not process any more measurements } if (Measurements.Count > 1) { // Keep only one measurement between 2 consecutive "0"s // - A valid measurement will be the last Stable measurement before a Stable "0" // - Glitches should be filtered out in code above this function // If no start detected insert one zero measurement artificially if (!(Measurements[0].TotalWeight == 0)) { var item = new Measurement { TotalWeight = 0, IsStable = true }; Measurements.Insert(0, item); } // Identify Start and end of measurement positions. (Measurements[] contains only "stable" measurements ) while (Measurements.Count > 1) // Start supposed at [0] { measurementStartPosition = 0; // Look for the position of next "0" measurement (measurementEndPosition), // ignoring all leading "0" by increasing measurementStartPosition for (int i = 1; i < Measurements.Count; i++) { // Ignore all duplicate leading "0" if ((i == measurementStartPosition + 1) && (Measurements[i].TotalWeight == 0)) { measurementStartPosition = i; } else { if (Measurements[i].TotalWeight == 0) // if end of measurement found { endOfMeasurementFound = true; measurementEndPosition = i - 1; break; } } } // If next "0" found, save last stable measurement and discard everything else if (endOfMeasurementFound) { var measurement = Measurements[measurementEndPosition]; // Sanity Check at the end with expected value if (measurement.TotalWeight > (netWeight - measurementTolerance) && measurement.TotalWeight < (netWeight + measurementTolerance)) { validMeasurements.Add(measurement); } else { log.Error("Measurement not within tollerance: " + measurement.TotalWeight); } // clear Measuremetns array for the processed measurements for (int i = 0; i <= measurementEndPosition; i++) { Measurements.RemoveAt(0); } endOfMeasurementFound = false; } else { break; } } if (stopPressed) { return; // Do not process any more measurements. Stop could be pressed in middle of loop } // Add all valid measurements to DataTable and excel if (validMeasurements.Count > 0) { AddValidMeasurementToDataTableAndExcel(validMeasurements); } } if (dataGridViewMeasurements.RowCount > 0) { dataGridViewMeasurements.FirstDisplayedScrollingRowIndex = dataGridViewMeasurements.RowCount - 1; } dataGridViewMeasurements.Refresh(); // })); }
public void Insert(int index, ListItem item) { _backingList.Insert(index, item); }
private void _queue(FileOperation operation, bool toTop) { operation.Owner = this; operation.ScheduledTime = DateTime.UtcNow; operation.OperationStatus = FileOperationStatus.Waiting; Logger.Info("Operation scheduled: {0}", operation); NotifyOperation(OperationAdded, operation); if ((operation.Kind == TFileOperationKind.Copy || operation.Kind == TFileOperationKind.Move || operation.Kind == TFileOperationKind.Convert)) { IMedia destMedia = operation.DestMedia; if (destMedia != null) { destMedia.MediaStatus = TMediaStatus.CopyPending; } } if (operation.Kind == TFileOperationKind.Convert) { lock (_queueConvertOperation.SyncRoot) { if (toTop) { _queueConvertOperation.Insert(0, operation); } else { _queueConvertOperation.Add(operation); } if (!_isRunningConvertOperation) { _isRunningConvertOperation = true; ThreadPool.QueueUserWorkItem(o => _runOperation(_queueConvertOperation, ref _isRunningConvertOperation)); } } } if (operation.Kind == TFileOperationKind.Export) { lock (_queueExportOperation.SyncRoot) { if (toTop) { _queueExportOperation.Insert(0, operation); } else { _queueExportOperation.Add(operation); } if (!_isRunningExportOperation) { _isRunningExportOperation = true; ThreadPool.QueueUserWorkItem(o => _runOperation(_queueExportOperation, ref _isRunningExportOperation)); } } } if (operation.Kind == TFileOperationKind.Copy || operation.Kind == TFileOperationKind.Delete || operation.Kind == TFileOperationKind.Loudness || operation.Kind == TFileOperationKind.Move) { lock (_queueSimpleOperation.SyncRoot) { if (toTop) { _queueSimpleOperation.Insert(0, operation); } else { _queueSimpleOperation.Add(operation); } if (!_isRunningSimpleOperation) { _isRunningSimpleOperation = true; ThreadPool.QueueUserWorkItem(o => _runOperation(_queueSimpleOperation, ref _isRunningSimpleOperation)); } } } }
// This Unit test is based from full framework test public static void SynchronizedCollectionPublicMembersTest() { SynchronizedCollection <int> coll = new SynchronizedCollection <int>(); int size = 100; for (int i = 0; i < size; i++) { coll.Add(i); } Assert.True(coll.Count == size, string.Format("collection count was wrong! Expected: {0} got: {1}", size, coll.Count)); for (int i = 0; i < size; i++) { Assert.True(coll[i] == i, string.Format("coll element {0} was wrong! Expected: {1} got: {2} ", i, i, coll[i])); Assert.True(coll.IndexOf(i) == i, string.Format("coll IndexOf wasn't right! Expected: {0} got: {1}", i, coll.IndexOf(i))); Assert.True(coll.Contains(i), string.Format("coll Contains failed to find the value {0}.", i)); } SynchronizedCollection <int> coll2 = new SynchronizedCollection <int>(new object(), new List <int>(coll)); for (int i = 0; i < size; i++) { Assert.True(coll2[i] == i, string.Format("coll2 element was wrong! expected: {0} got: {1} ", i, coll2[i])); } SynchronizedCollection <int> coll3 = new SynchronizedCollection <int>(new object(), 1, 2, 3, 4, 5, 6); for (int i = 0; i < 5; i++) { Assert.True(coll3[i] == i + 1, string.Format("coll3 element {0} was wrong! expected: {1} got: {2}", i, i + 1, coll3[i])); } int newValue = 80; coll3[5] = newValue; Assert.True(coll3[5] == newValue); IEnumerator <int> e = coll.GetEnumerator(); int n = 0; while (e.MoveNext()) { Assert.True(e.Current.Equals(n++), string.Format("Expected: {0}, got:{1}", n - 1, e.Current)); } Assert.True(n == 100, string.Format("Expect number of elements: {0}, got:{1}", 100, n)); int[] array = new int[size + 1]; coll.CopyTo(array, 1); for (int i = 0; i < size; i++) { Assert.True(array[i + 1] == i, string.Format("After CopyTo, Element {0} was wrong! Expected: {1} got: {2}", i, i + 1, array[i + 1])); } coll.Add(coll.Count); coll.Insert(0, -1); coll.RemoveAt(0); coll.Remove(coll.Count - 1); Assert.True(coll.Count == size, string.Format("Expect number of elements after modification: {0}, got: {1}", size, coll.Count)); for (int i = 0; i < size; i++) { Assert.True(coll[i] == i, string.Format("coll element was wrong after modification! Expected: {0} got: {1} ", i, coll[i])); } coll.Clear(); Assert.True(coll.Count == 0, string.Format("Clear operation failed!, expected: 0, actual {0}", coll.Count)); // Negative cases Assert.Throws <ArgumentNullException>("syncRoot", () => { new SynchronizedCollection <int>(null); }); Assert.Throws <ArgumentNullException>("list", () => { new SynchronizedCollection <int>(new object(), null); }); Assert.Throws <ArgumentNullException>("syncRoot", () => { new SynchronizedCollection <int>(null, new List <int>()); }); Assert.Throws <ArgumentNullException>("syncRoot", () => { new SynchronizedCollection <int>(null, 1, 2, 3, 4); }); Assert.Throws <ArgumentOutOfRangeException>(() => { coll[1000] = 5; }); Assert.Throws <ArgumentOutOfRangeException>(() => { coll[-1] = 5; }); Assert.Throws <ArgumentOutOfRangeException>(() => { coll.Insert(1000, 5); }); Assert.Throws <ArgumentOutOfRangeException>(() => { coll.Insert(-1, 5); }); Assert.False(coll.Remove(100000)); Assert.Throws <ArgumentOutOfRangeException>(() => { coll.RemoveAt(-1); }); Assert.Throws <ArgumentOutOfRangeException>(() => { coll.RemoveAt(10000); }); }
private void InsertItemInList(ListBox2 parent, ListItem item, int index) { m_list.Insert(index, item); RefreshParent(parent); }
/// <summary> /// Advance the lane to the next element /// </summary> /// <param name="curLane">Index of the lane to advance</param> /// <returns>True if there will still be nodes in this lane</returns> private int AdvanceLane(int curLane) { LaneJunctionDetail lane = _laneNodes[curLane]; int minLane = curLane; // Advance the lane lane.Next(); // See if we can pull up ancestors if (lane.Count == 0 && lane.Junction == null) { // Handle a single node branch. _currentRow.Collapse(curLane); _laneNodes.RemoveAt(curLane); } else if (lane.Count == 0) { Node node = lane.Junction.Oldest; foreach (Junction parent in node.Ancestors) { if (parent.ProcessingState != JunctionProcessingState.Unprocessed) { // This item is already in the lane list, no action needed continue; } var addedLane = new LaneJunctionDetail(parent); addedLane.Next(); int addedLaneLane = int.MaxValue; // Check to see if this junction already points to one of the // existing lanes. If so, we'll just add the lane line and not // add it to the laneNodes. if (addedLane.Count == 1) { for (int i = 0; i < _laneNodes.Count; i++) { if (_laneNodes[i].Current == addedLane.Current) { // We still advance the lane so it gets // marked as processed. addedLane.Next(); addedLaneLane = i; break; } } } // Add to the lane nodes if (addedLaneLane == int.MaxValue) { if (lane.Count == 0) { lane = addedLane; _laneNodes[curLane] = lane; addedLaneLane = curLane; } else { addedLaneLane = curLane + 1; _laneNodes.Insert(addedLaneLane, addedLane); _currentRow.Expand(addedLaneLane); } } _currentRow.Add(curLane, new LaneInfo(addedLaneLane, parent)); } // If the lane count after processing is still 0 // this is a root node of the graph if (lane.Count == 0) { _currentRow.Collapse(curLane); _laneNodes.RemoveAt(curLane); } } else if (lane.Count == 1) { // If any other lanes have this node on top, merge them together for (int i = 0; i < _laneNodes.Count; i++) { if (i == curLane || curLane >= _laneNodes.Count) { continue; } if (_laneNodes[i].Current == _laneNodes[curLane].Current) { int left; int right; Junction junction = _laneNodes[curLane].Junction; if (i > curLane) { left = curLane; right = i; } else { left = i; right = curLane; curLane = i; } _currentRow.Replace(right, left); _currentRow.Collapse(right); _laneNodes[right].Clear(); _laneNodes.RemoveAt(right); _currentRow.Add(_currentRow.NodeLane, new LaneInfo(left, junction)); minLane = Math.Min(minLane, left); } } // If the current lane is still active, add it. It might not be active // if it got merged above. if (!lane.IsClear) { _currentRow.Add(_currentRow.NodeLane, new LaneInfo(curLane, lane.Junction)); } } else { // lane.Count > 1 _currentRow.Add(_currentRow.NodeLane, new LaneInfo(curLane, lane.Junction)); } return(curLane); }
// This Unit test is based from full framework test public static void SynchronizedCollectionPublicMembersTest() { SynchronizedCollection<int> coll = new SynchronizedCollection<int>(); int size = 100; for (int i = 0; i < size; i++) coll.Add(i); Assert.True(coll.Count == size, string.Format("collection count was wrong! Expected: {0} got: {1}", size, coll.Count)); for (int i = 0; i < size; i++) { Assert.True(coll[i] == i, string.Format("coll element {0} was wrong! Expected: {1} got: {2} ", i, i, coll[i])); Assert.True(coll.IndexOf(i) == i, string.Format("coll IndexOf wasn't right! Expected: {0} got: {1}" , i, coll.IndexOf(i))); Assert.True(coll.Contains(i), string.Format("coll Contains failed to find the value {0}.", i)); } SynchronizedCollection<int> coll2 = new SynchronizedCollection<int>(new object(), new List<int>(coll)); for (int i = 0; i < size; i++) { Assert.True(coll2[i] == i, string.Format("coll2 element was wrong! expected: {0} got: {1} ", i, coll2[i])); } SynchronizedCollection<int> coll3 = new SynchronizedCollection<int>(new object(), 1, 2, 3, 4, 5 , 6); for (int i = 0; i < 5; i++) { Assert.True(coll3[i] == i + 1, string.Format("coll3 element {0} was wrong! expected: {1} got: {2}", i, i+1, coll3[i])); } int newValue = 80; coll3[5] = newValue; Assert.True(coll3[5] == newValue); IEnumerator <int> e = coll.GetEnumerator(); int n = 0; while (e.MoveNext()) { Assert.True(e.Current.Equals(n++), string.Format("Expected: {0}, got:{1}", n-1, e.Current)); } Assert.True(n == 100, string.Format("Expect number of elements: {0}, got:{1}", 100, n)); int[] array = new int[size + 1]; coll.CopyTo(array, 1); for (int i = 0; i < size; i++) { Assert.True(array[i + 1] == i, string.Format("After CopyTo, Element {0} was wrong! Expected: {1} got: {2}", i, i+1, array[i + 1])); } coll.Add(coll.Count); coll.Insert(0, -1); coll.RemoveAt(0); coll.Remove(coll.Count - 1); Assert.True(coll.Count == size, string.Format("Expect number of elements after modification: {0}, got: {1}", size, coll.Count)); for (int i = 0; i < size; i++) { Assert.True(coll[i] == i, string.Format("coll element was wrong after modification! Expected: {0} got: {1} ", i, coll[i])); } coll.Clear(); Assert.True(coll.Count == 0, string.Format("Clear operation failed!, expected: 0, actual {0}", coll.Count)); // Negative cases Assert.Throws<ArgumentNullException>("syncRoot", () => { new SynchronizedCollection<int>(null); }); Assert.Throws<ArgumentNullException>("list", () => { new SynchronizedCollection<int>(new object(), null); }); Assert.Throws<ArgumentNullException>("syncRoot", () => { new SynchronizedCollection<int>(null, new List<int>()); }); Assert.Throws<ArgumentNullException>("syncRoot", () => { new SynchronizedCollection<int>(null, 1, 2, 3, 4); }); Assert.Throws<ArgumentOutOfRangeException>(() => { coll[1000] = 5; }); Assert.Throws<ArgumentOutOfRangeException>(() => { coll[-1] = 5; }); Assert.Throws<ArgumentOutOfRangeException>(() => { coll.Insert(1000, 5); }); Assert.Throws<ArgumentOutOfRangeException>(() => { coll.Insert(-1, 5); }); Assert.False(coll.Remove(100000)); Assert.Throws<ArgumentOutOfRangeException>(() => { coll.RemoveAt(-1); }); Assert.Throws<ArgumentOutOfRangeException>(() => { coll.RemoveAt(10000); }); }