I am sure this is pretty standard stuff, but I can’t seem to wrap my head around this.
I’m writing a simple helper class for executing code in separate threads. I’m very familiar with how to do this in general, but not using lambdas. I guess this is about general lambda usage, rather than threads.
What I want to be able to write is something like this:
ThreadedMethods.Parameterized<int>((i) => { for (;i < 10; i++;) DoSomething();});
and
ThreadedMethods.Parameterized<IEnumberable<Something>>((list) =>
{
foreach (var s in list)
{
s.SomeHeavyProcessing();
}
});
Is this possible? And, moreover, is there any reason to try to achieve this in the first place, since the lambda will have access to everything within the scope of the method call?
Sure, it is possible. You should have implementation for
Then you call your implementation as you have shown with your examples in your question.
EDIT: More info on how you can call the ThreadPool.QueueUserWorkItem method when you have Action