Rather new to threading. I have a situation like this:
class Program
{
    static void Main(string[] args)
    {
        List<NewsItem> newsItems = new List<NewsItem>();
        for (int i = 0; i < 5; i++)
        {
            NewsItem item = new NewsItem(i.ToString());
            newsItems.Add(item);
        }
        List<Thread> workerThreads = new List<Thread>();
        foreach (NewsItem article in newsItems)
        {
            Console.WriteLine("Dispatching: " + article.Headline);
            Thread thread = new Thread(() =>
            {
                Console.WriteLine("In thread:" + article.Headline);
            });
            workerThreads.Add(thread);
            thread.Start();
        }
        foreach (Thread thread in workerThreads)
        {
            thread.Join();
        }
        Console.ReadKey();
    }
}
class NewsItem
{
    public string Headline { get; set; }
    public NewsItem(string headline)
    {
        Headline = headline;
    }
}
Which invariably, when run, gives me this:
Dispatching: 0
Dispatching: 1
Dispatching: 2
In thread: 2
Dispatching: 3
In thread: 3
In thread: 2
Dispatching: 4
In thread: 4
In thread: 4
In other words, the thread parameter is not what I would expect.
I'm guessing I could get around this by using a ParameterizedThreadStart delegate instead of an anonymous lambda expression, but I'm still curious to know why this works as it does (or rahter, doesn't work as expected)?
Thanks!
Steve
