How to get thread Id in C#

Recawo picture Recawo · Apr 19, 2012 · Viewed 14.9k times · Source
public bool HasItemsFromPropertySet(InfoItemPropertySet propertySet, CompositeInfoItem itemRemoved)
    {
        var itemAndSubItems = new InfoItemCollection();
        if (itemRemoved != null)
        {
            itemAndSubItems.Add(itemRemoved);
            //foreach (InfoItem item in itemRemoved.AllDescendants)
            itemAndSubItems.AddRange(itemRemoved.AllDescendants);
        }
        return AllItems.AsParallel().Any(item => item.PropertySet == propertySet && !itemAndSubItems.Contains(item));
    }


Above in my code I use AsParallel().Any() How can i get thread ID of thread generated by that AsParellel.Any()...

Answer

Martin Liversage picture Martin Liversage · Apr 19, 2012

Thread.CurrentThread.ManagedThreadId gets the managed thread ID of the currently executing thread.

If you want to get the native thread ID instead (not something you normally want to do) you can call the method AppDomain.GetCurrentThreadId() (obsoleted "because it does not provide a stable Id when managed threads are running on fibers" but as far as I know managed threads are only running on fibers inside SQL Server).