The best option is probably to use a lambda expression that closes over the variables you want to display.
However, be careful in this case, especially if you're calling this in a loop. (I mention this since your variable is an "ID", and this is common in this situation.) If you close over the variable in the wrong scope, you can get a bug. For details, see Eric Lippert's post on the subject. This typically requires making a temporary:
foreach(int id in myIdsToCheck)
{
int tempId = id; // Make a temporary here!
Task.Factory.StartNew( () => CheckFiles(tempId, theBlockingCollection),
cancelCheckFile.Token,
TaskCreationOptions.LongRunning,
TaskScheduler.Default);
}
Also, if your code is like the above, you should be careful with using the LongRunning
hint - with the default scheduler, this causes each task to get its own dedicated thread instead of using the ThreadPool. If you're creating many tasks, this is likely to have a negative impact as you won't get the advantages of the ThreadPool. It's typically geared for a single, long running task (hence its name), not something that would be implemented to work on an item of a collection, etc.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…