I'm attempting to write some multi-threaded code in C# XNA. What I want to do is move a for-loop calculation into a new thread. I'm using the ThreadStart() method in XNA to do this. However, while I can move the for-loop into a new thread, it's part of a method that takes in variables and while in the thread, the for loop in unable to use variables outside of the thread.
public static string EndianFlip32BitChunks(string input)
{
//32 bits = 4*4 bytes = 4*4*2 chars
string result = "";
ThreadStart threadStarter = delegate
{
for (int i = 0; i < input.Length; i += 8)
for (int j = 0; j < 8; j += 2)
{
//append byte (2 chars)
result += input[i - j + 6];
result += input[i - j + 7];
}
};
Thread loadingThread = new Thread(threadStarter);
loadingThread.Start();
return result;
}
Basically, I'm wondering how am I going to get variables from outside the thread, into the thread. These variables may be changing too. In the case of the code displayed above, the variable I need to use is the string result. The code works if the string is left outside the thread, but then the thread only reads the initial value and never updates that value.