how can I force the task to use the first value and not the second value. This is a simpler example of code that has become a problem for me.
static void Main(string[] args)
{
string testString = "1 value";
Task.Factory.StartNew(() => PrintStr(testString));
testString = "2 value";
Console.ReadLine();
}
static void PrintStr(string str)
{
Console.WriteLine(str);
}
In response I get "2 value"
The problem is that I need to pass a string to the task, after which this string will be changed and I cannot guarantee. that the task will receive the correct information.
I have a collection of posts, for each post I create a task and I need to pass the html markup in a string variable. For each step of the loop, this variable will change, and therefore I cannot be sure that the task will receive the correct data. Sample code:
foreach(var uid in uids) // collection of messages
{
message = imapClient.Inbox.GetMessage(uid);
var htmlData = message.HtmlBody.ToString();
tasks.Add(Task.Factory.StartNew(() => someHandle(htmlData));
}
void someHandle(string data)
{
//some code to handle the message
}