public static void CopyTo_Empty_NothingCopied() { var bag = new ConcurrentBag <int>(); bag.CopyTo(new int[0], 0); bag.CopyTo(new int[10], 10); }
public static void CopyTo_TypeMismatch() { const int Size = 10; var c = new ConcurrentBag <Exception>(Enumerable.Range(0, Size).Select(_ => new Exception())); c.CopyTo(new Exception[Size], 0); Assert.Throws <InvalidCastException>(() => c.CopyTo(new InvalidOperationException[Size], 0)); }
public static void ICollectionCopyTo_InvalidArgs_Throws() { ICollection bag = new ConcurrentBag <int>(Enumerable.Range(0, 10)); Array dest = new int[10]; Assert.Throws <ArgumentNullException>("array", () => bag.CopyTo(null, 0)); Assert.Throws <ArgumentOutOfRangeException>("dstIndex", () => bag.CopyTo(dest, -1)); Assert.Throws <ArgumentException>(() => bag.CopyTo(dest, dest.Length)); Assert.Throws <ArgumentException>(() => bag.CopyTo(dest, dest.Length - 2)); }
public static void RTest5_CopyTo() { const int SIZE = 10; Array array = new int[SIZE]; int index = 0; ConcurrentBag <int> bag = CreateBag(SIZE); bag.CopyTo((int[])array, index); Assert.Throws <ArgumentNullException>(() => bag.CopyTo(null, index)); Assert.Throws <ArgumentOutOfRangeException>(() => bag.CopyTo((int[])array, -1)); Assert.Throws <ArgumentException>(() => bag.CopyTo((int[])array, SIZE)); Assert.Throws <ArgumentException>(() => bag.CopyTo((int[])array, SIZE - 2)); }
/// <summary> /// Copies the <see cref="T:System.Collections.Concurrent.ConcurrentBag`1"/> elements to an existing one-dimensional <see cref="T:System.Array"/>, starting at the specified array index. /// </summary> /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from the <see cref="T:System.Collections.Concurrent.ConcurrentBag`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param><param name="index">The zero-based index in <paramref name="array"/> at which copying begins.</param><exception cref="T:System.ArgumentNullException"><paramref name="array"/> is a null reference (Nothing in Visual Basic).</exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is less than zero.</exception><exception cref="T:System.ArgumentException"><paramref name="index"/> is equal to or greater than the length of the <paramref name="array"/> -or- the number of elements in the source <see cref="T:System.Collections.Concurrent.ConcurrentBag`1"/> is greater than the available space from <paramref name="index"/> to the end of the destination <paramref name="array"/>.</exception> void IProducerConsumerCollection <T> .CopyTo(T[] array, int index) { lock (((ICollection)this).SyncRoot) { _concurrentBag.CopyTo(array, index); } }
/// <summary> /// Copies the pool elements to an existing one-dimensional array, starting at the /// specified offset. /// </summary> /// <param name="array"> /// The one-dimensional array of awaitable socket arguments that is the destination of /// the arguments copied from the pool. Array must have zero-based indexing. /// </param> /// <param name="offset"> /// The zero-based index in <paramref name="array" /> of which copying begins. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="array" /> is null. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="offset" /> is less than zero. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="array" /> is not a single-dimensional array of /// <see cref="SocketAwaitable" /> instances. /// -or- /// <paramref name="offset" /> is equal to or greater than the length of /// <paramref name="array" /> /// -or- /// The number of elements in the source pool is greater than the available space from /// <paramref name="offset" /> to the end of <paramref name="array" />. /// </exception> /// <exception cref="ObjectDisposedException"> /// The <see cref="SocketAwaitablePool" /> has been disposed. /// </exception> void ICollection.CopyTo(Array array, int offset) { if (array == null) { throw new ArgumentNullException("array", "Array must not be null."); } if (offset < 0) { throw new ArgumentOutOfRangeException("index", offset, "Index must not be null."); } if (!(array is SocketAwaitable[])) { var message = string.Format( "Array must be a single-dimensional array of `{0}`.", typeof(SocketAwaitable).FullName); throw new ArgumentException(message, "array"); } lock (_bag) if (!IsDisposed) { _bag.CopyTo(array as SocketAwaitable[], offset); } else { throw new ObjectDisposedException(TypeName); } }
//private static Timer _timer2 = // new Timer(CheckSubs, null, 1000, 1000); //private static void CheckSubs(object state) //{ // var brokenSubs = Level2List.Where(x => x.Level2Data.Count == 0).ToList(); // foreach (var sub in brokenSubs) // { // TXmlConnector.ConnectorSendCommand( // ConnectorCommands.SubUnsubCommand("subscribe", "quotes", sub.Board, sub.Seccode)); // } //} private static void ClearSubs(object state) { var windows = _dispatcher.Invoke(() => Application.Current.Windows); List <IAnchor> list = new List <IAnchor>(); foreach (Window window in windows) { var context = _dispatcher.Invoke(() => window.DataContext); if (context is IAnchor) { list.Add((IAnchor)context); } } Level2Subscription[] arr = new Level2Subscription[Level2List.Count]; Level2List.CopyTo(arr, 0); foreach (var level2Subscription in arr) { var sub = list.Count(c => c.Board == level2Subscription.Board && c.Seccode == level2Subscription.Seccode); if (sub == 0) { TXmlConnector.ConnectorSendCommand( ConnectorCommands.SubUnsubCommand("unsubscribe", "quotes", level2Subscription.Board, level2Subscription.Seccode)); var subscription = level2Subscription; Level2List.TryTake(out subscription); } } }
//private static Timer _timer2 = new Timer(CheckSubs, null, 1000, 1000); //private static void CheckSubs(object state) //{ // var brokenSubs = TickList.Where(x => x.Ticks.Count == 0).ToList(); // foreach (var sub in brokenSubs) // { // sub.Tradeno = 1; // } // TXmlConnector.ConnectorSendCommand(ReturnTickSubsXml()); // foreach (var sub in brokenSubs) // { // sub.Tradeno = 0; // } //} private static void ClearSubs(object state) { var windows = _dispatcher.Invoke(() => Application.Current.Windows); List <IAnchor> list = new List <IAnchor>(); foreach (Window window in windows) { var context = _dispatcher.Invoke(() => window.DataContext); if (context is IAnchor) { list.Add((IAnchor)context); } } TickSubscription[] arr = new TickSubscription[TickList.Count]; TickList.CopyTo(arr, 0); foreach (var tickSubscription in arr) { var sub = list.Count(c => c.Board == tickSubscription.Board && c.Seccode == tickSubscription.Seccode); if (sub == 0) { var subscription = tickSubscription; TickList.TryTake(out subscription); TXmlConnector.ConnectorSendCommand(ReturnTickSubsXml()); } } }
public void Dispose() { MouseEventDispatcher.OnMousePress -= OnMousePress; MouseEventDispatcher.OnMouseRelease -= OnMouseRelease; MouseEventDispatcher.OnMouseMotion -= OnMouseMove; // Dispose All In The List IDisposable[] td = new IDisposable[toDispose.Count]; toDispose.CopyTo(td, 0); for (int i = 0; i < td.Length; i++) { td[i].Dispose(); td[i] = null; } }
public static void ICollectionCopyTo_ExpectedElementsCopied() { const int Size = 10; int[] dest; // Copy to array in which data fits perfectly dest = new int[Size]; ICollection c = new ConcurrentBag <int>(Enumerable.Range(0, Size)); c.CopyTo(dest, 0); Assert.Equal(Enumerable.Range(0, Size), dest.OrderBy(i => i)); // Copy to non-0 index in array where the data fits dest = new int[Size + 3]; c = new ConcurrentBag <int>(Enumerable.Range(0, Size)); c.CopyTo(dest, 1); var results = new int[Size]; Array.Copy(dest, 1, results, 0, results.Length); Assert.Equal(Enumerable.Range(0, Size), results.OrderBy(i => i)); }
public void CopyTo(T[] array, int arrayIndex) => collection.CopyTo(array, arrayIndex);
static void Main(string[] args) { // CONSTRUCTORS ConcurrentBag <int> cbag = new ConcurrentBag <int>(new int[] { 1, 2, 4, 7, 12, 222 }); ConcurrentBag <string> cb = new ConcurrentBag <string>(); // PROPERTIES Console.WriteLine("Count = {0}", cb.Count); // 0 Console.WriteLine("Is cb empty? {0}", cb.IsEmpty); // true // FUNCTIONS cb.Add(item: "cow"); cb.Add(item: "chicken"); cb.Add(item: "duck"); cb.Add(item: "sheep"); Console.WriteLine("cb = "); foreach (string s in cb) { Console.Write("{0}, ", s); } // {"cow", "chicken", "duck", "sheep"} Console.WriteLine(); // cb.Clear(); cb = new ConcurrentBag <string>(); Console.WriteLine("cb after clear is empty? {0}", cb.IsEmpty); // true cb.Add(item: "Germany"); cb.Add(item: "Spain"); cb.Add(item: "Sweden"); cb.Add(item: "Serbia"); string[] arr = new string[cb.Count]; cb.CopyTo(array: arr, index: 0); Console.WriteLine("arr = "); foreach (string s in arr) { Console.Write("{0}, ", s); } // {"Germany", "Spain", "Sweden", "Serbia"} Console.WriteLine(); ConcurrentBag <string> cb_copy = new ConcurrentBag <string>(cb); Console.WriteLine("cb is equal to its copy? {0}", cb.Equals(cb_copy)); // true IEnumerator <string> en = cb.GetEnumerator(); Console.Write("cb = "); while (en.MoveNext()) { Console.Write("{0}, ", en.Current); } Console.WriteLine(); Console.WriteLine("cb hashcode = {0}", cb.GetHashCode()); Console.WriteLine("cb type = {0}", cb.GetType().FullName); arr = cb.ToArray(); Console.WriteLine("arr afteer using ToArray on cb = "); foreach (string s in arr) { Console.Write("{0}, ", s); } // {"Germany", "Spain", "Sweden", "Serbia"} Console.WriteLine(); Console.WriteLine("string version of cb = {0}", cb.ToString()); string result_str; Console.WriteLine("peeking = {0}", cb.TryPeek(result: out result_str)); // true Console.WriteLine("peeking result = {0}", result_str); // Console.WriteLine("taking = {0}", cb.TryTaking(result: out result_str)); // true // Console.WriteLine("taking result = {0}", result_str); // Threading int num = 0; Thread threadAdd = new Thread(() => { for (int i = num; i < num + 10; i++) { cb.Add(item: i.ToString()); Console.WriteLine("threadAdd: {0}", i); } num += 10; Console.WriteLine(); }); Thread threadAddWord = new Thread(() => { for (int i = 1; i < 10; i++) { cb.Add(item: "Word"); Console.WriteLine("threadAddWord: {0}", i); } Console.WriteLine(); }); Thread threadFinal = new Thread(() => { Console.WriteLine("cb after threading = "); foreach (string s in cb) { Console.Write("{0}, ", s); } Console.WriteLine(); }); threadAdd.Start(); threadAddWord.Start(); threadAdd.Join(); threadAddWord.Join(); threadFinal.Start(); }
public override void CopyTo(SearchItem[] array, int arrayIndex) { m_UnorderedItems.CopyTo(array, arrayIndex); }
public void CopyTo(T[] array, int arrayIndex) { _pool.CopyTo(array, arrayIndex); }
public static void CopyTo <T>(ConcurrentBag <T> concurrentBag, T[] array, int index) { ConcurrentCollectionHelper.Interleave(); concurrentBag.CopyTo(array, index); }
public void CopyTo(CacheKeyMapDescriptor[] array, int arrayIndex) { mapList.CopyTo(array, arrayIndex); }