(environment: Azure websites. Own, non-shared 1 core instance) I have the following loop which spawns needed number of threads.
clientIds = comUtility.getAllSyncableClientIds();
int numOfThreads = clientIds.Count();
Thread[] exportThreads = new Thread[numOfThreads];
foreach (int clientId in clientIds)
{
offset = offset + 10000; // time to wait
exportThreads[threadId] = new Thread(() => RunExportToAzureOnItsOwnThread(pId, clientId, offset, threadId));
exportThreads[threadId].Start();
threadId++;
}
Most of the time it seems to work fine. All threads spawn one after another and each thread has its own offset parameter which is just a wait time value called in each individual thread Thread.Sleep(offset); See the results from the log below:
HOWEVER, FOR SOME REASON, ONCE IN A WHILE some of the threads spawn with the same thread number and offset value even though the parameter clientId is supplied properly in the chronological order (see below).
What I do not understand is how could the foreach loop finish with only 1/2 of the functionality within the loop being correct - in this case the loop correctly fetches the next clientId but forgets to increase the offset value.
Thx.