Why would this happen?
You start up a thread, and it runs a class.
Inside that class there is a for loop, inside the for loop there is a “write to messagebox” and another class.
Why does the order in witch I put the write and the class (which one happens first) effect the accuracy of my data?
(This is inside the class that the thread is running)
int atPDFNumber= 0
foreach (var z in q)
{
atPDFNumber++;
convertToImage(z.FullName);
txtboxtest.BeginInvoke(
((Action)(() => txtboxtest.Text += atPDFNumber.ToString())));
}
That output gives me some overlapping values. the output is
Run 1 : 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Run 2 : 1 3 3 4 5 6 7 8 9 10 11 12 13 14
Run 2 : 1 2 3 4 5 6 7 8 9 11 11 12 13 14
Where this Runs them all in the correct order
foreach (var z in q)
{
atPDFNumber++;
txtboxtest.BeginInvoke(
((Action)(() => txtboxtest.Text += atPDFNumber.ToString())));
convertToImage(z.FullName);
Why does that happen? It might be that the external method i use in the class ConvertToImage is also running a thread.. in that case, how would i “pause” my thread to wait for the ConvertToImage to finish…
Other wise i have no idea why this thread acts the way it does?
Your action is capturing the value of atPDFNumber variable, not the current value of it. In your example if you assing atPDFNumber to a local variable and pass it to your action it will work as expected.
Here you can find about Variable Capturing in C#