/// <summary> /// Returns the list of thread id of all the threads of the current process /// </summary> /// <returns></returns> private List <int> GetCurrentThreadIds() { ProcessThreadCollection threadCollection = Process.GetCurrentProcess().Threads; IEnumerator e = threadCollection.GetEnumerator(); List <int> IdList = new List <int>(); while (e.MoveNext()) { ProcessThread pt = (ProcessThread)e.Current; IdList.Add(pt.Id); } return(IdList); }
/// <summary> /// Returns the ProcessThread corresponding to given thread id /// </summary> /// <param name="id"></param> /// <returns></returns> private ProcessThread GetProcessorThreadByID(int id) { ProcessThreadCollection threadCollection = Process.GetCurrentProcess().Threads; IEnumerator e = threadCollection.GetEnumerator(); ProcessThread processthread = null; while (e.MoveNext()) { ProcessThread pt = (ProcessThread)e.Current; if (pt.Id == id) { processthread = pt; break; } } return(processthread); }
/// <summary> /// Moves all the existing running threads on first processor /// </summary> private void SetAffinityForAllThreads() { ProcessThreadCollection collection = Process.GetCurrentProcess().Threads; IEnumerator e = collection.GetEnumerator(); int x = 1 << (0); while (e.MoveNext()) { try { ProcessThread pt = (ProcessThread)e.Current; pt.ProcessorAffinity = new IntPtr(x); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
public void TestThreadCollectionBehavior() { ProcessThread[] tArray = _process.Threads.Cast<ProcessThread>().ToArray(); int countOfTArray = tArray.Count(); // constructor ProcessThreadCollection threadCollection = new ProcessThreadCollection(tArray); // Count Assert.Equal(countOfTArray, threadCollection.Count); // get_item, Contains, IndexOf for (int i = 0; i < countOfTArray; i++) { Assert.Equal(tArray[i], threadCollection[i]); Assert.True(threadCollection.Contains(tArray[i])); Assert.Equal(i, threadCollection.IndexOf(tArray[i])); } // CopyTo ProcessThread[] threadArray = new ProcessThread[threadCollection.Count + 1]; threadCollection.CopyTo(threadArray, 1); for (int i = 0; i < countOfTArray; i++) { Assert.Equal(tArray[i], threadArray[i + 1]); } Assert.Throws<ArgumentOutOfRangeException>(() => threadCollection.CopyTo(threadArray, -1)); // Remove threadCollection.Remove(tArray[0]); Assert.Equal(-1, threadCollection.IndexOf(tArray[0])); Assert.False(threadCollection.Contains(tArray[0])); // Try remove non existent member threadCollection.Remove(tArray[0]); // Cleanup after remove threadCollection.Insert(0, tArray[0]); // Add threadCollection.Add(default(ProcessThread)); Assert.Equal(threadCollection.Count - 1, threadCollection.IndexOf(default(ProcessThread))); // Add same member again threadCollection.Add(default(ProcessThread)); Assert.Equal(threadCollection.Count - 2, threadCollection.IndexOf(default(ProcessThread))); Assert.Equal(default(ProcessThread), threadCollection[threadCollection.Count - 1]); // Cleanup after Add. threadCollection.Remove(default(ProcessThread)); threadCollection.Remove(default(ProcessThread)); Assert.False(threadCollection.Contains(default(ProcessThread))); // Insert int index = threadCollection.Count / 2; int initialCount = threadCollection.Count; threadCollection.Insert(index, null); Assert.Equal(index, threadCollection.IndexOf(null)); Assert.Equal(initialCount + 1, threadCollection.Count); // Insert at invalid index Assert.Throws<ArgumentOutOfRangeException>(() => threadCollection.Insert(-1, tArray[0])); // Explicit interface implementations Assert.False(((ICollection)threadCollection).IsSynchronized); Assert.NotNull(((ICollection)threadCollection).SyncRoot); threadArray = new ProcessThread[threadCollection.Count]; ((ICollection)threadCollection).CopyTo(threadArray, 0); Assert.Equal(threadCollection.Cast<ProcessThread>().ToArray(), threadArray); // GetEnumerator IEnumerator enumerator = threadCollection.GetEnumerator(); Assert.Throws<InvalidOperationException>(() => enumerator.Current); for (int i = 0; i < threadCollection.Count; i++) { enumerator.MoveNext(); Assert.Equal(threadCollection[i], enumerator.Current); } }
public void TestThreadCollectionBehavior() { ProcessThread[] tArray = _process.Threads.Cast <ProcessThread>().ToArray(); int countOfTArray = tArray.Count(); // constructor ProcessThreadCollection threadCollection = new ProcessThreadCollection(tArray); // Count Assert.Equal(countOfTArray, threadCollection.Count); // get_item, Contains, IndexOf for (int i = 0; i < countOfTArray; i++) { Assert.Equal(tArray[i], threadCollection[i]); Assert.True(threadCollection.Contains(tArray[i])); Assert.Equal(i, threadCollection.IndexOf(tArray[i])); } // CopyTo ProcessThread[] threadArray = new ProcessThread[threadCollection.Count + 1]; threadCollection.CopyTo(threadArray, 1); for (int i = 0; i < countOfTArray; i++) { Assert.Equal(tArray[i], threadArray[i + 1]); } Assert.Throws <ArgumentOutOfRangeException>(() => threadCollection.CopyTo(threadArray, -1)); // Remove threadCollection.Remove(tArray[0]); Assert.Equal(-1, threadCollection.IndexOf(tArray[0])); Assert.False(threadCollection.Contains(tArray[0])); // Try remove non existent member threadCollection.Remove(tArray[0]); // Cleanup after remove threadCollection.Insert(0, tArray[0]); // Add threadCollection.Add(default(ProcessThread)); Assert.Equal(threadCollection.Count - 1, threadCollection.IndexOf(default(ProcessThread))); // Add same member again threadCollection.Add(default(ProcessThread)); Assert.Equal(threadCollection.Count - 2, threadCollection.IndexOf(default(ProcessThread))); Assert.Equal(default(ProcessThread), threadCollection[threadCollection.Count - 1]); // Cleanup after Add. threadCollection.Remove(default(ProcessThread)); threadCollection.Remove(default(ProcessThread)); Assert.False(threadCollection.Contains(default(ProcessThread))); // Insert int index = threadCollection.Count / 2; int initialCount = threadCollection.Count; threadCollection.Insert(index, null); Assert.Equal(index, threadCollection.IndexOf(null)); Assert.Equal(initialCount + 1, threadCollection.Count); // Insert at invalid index Assert.Throws <ArgumentOutOfRangeException>(() => threadCollection.Insert(-1, tArray[0])); // Explicit interface implementations Assert.False(((ICollection)threadCollection).IsSynchronized); Assert.NotNull(((ICollection)threadCollection).SyncRoot); threadArray = new ProcessThread[threadCollection.Count]; ((ICollection)threadCollection).CopyTo(threadArray, 0); Assert.Equal(threadCollection.Cast <ProcessThread>().ToArray(), threadArray); // GetEnumerator IEnumerator enumerator = threadCollection.GetEnumerator(); Assert.Throws <InvalidOperationException>(() => enumerator.Current); for (int i = 0; i < threadCollection.Count; i++) { enumerator.MoveNext(); Assert.Equal(threadCollection[i], enumerator.Current); } }