Very new to async programming and trying to test some things. Any clue what I need to do to ensure that my Debug.WriteLine() statements will print the "correct" value for "i" (from my original loop)? The below attempt (of assigning to x and passing that value down to my async method) didnt work (as i never get a 0 value and then usually get the last value, 5, repeated several times). Ive seen some posts about lock () and tried wrapping the assignment to x in it, but I clearly dont understand exactly how that works, and it didnt seem to in this case. TIA
private async void btnStart_Click(object sender, EventArgs e)
{
var tskDataSetCodes = new List<Task>();
Stopwatch s = new Stopwatch();
int x = 99;
for (int i = 0; i < 6; i++)
{
x = i;
s.Start();
Task kickoffTasks = new Task(() => ExtractDataSetCode(x));
System.Diagnostics.Debug.WriteLine("DataSet : " + x + "Task: " + kickoffTasks.Id);
kickoffTasks.Start();
tskDataSetCodes.Add(kickoffTasks);
}
await Task.WhenAll(tskDataSetCodes);
s.Stop();
var tasktime = s.ElapsedMilliseconds;
System.Diagnostics.Debug.WriteLine("Application Completed" + tasktime);
}
static async void ExtractDataSetCode(int a)
{
System.Diagnostics.Debug.WriteLine("ExtractDataSetCode: " + a + " on thread: " + Thread.CurrentThread.ManagedThreadId);
var tasks = new List<Task>();
for (var count = 1; count <= 10; ++count)
{
Task queryTasks = new Task(() => queryProcess(a), TaskCreationOptions.AttachedToParent);
queryTasks.Start();
tasks.Add(queryTasks);
}
await Task.WhenAll(tasks);
}
static void queryProcess(int z)
{
System.Diagnostics.Debug.WriteLine("Starting queryProcess for dataset: " + z + " on thread: " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(10000);
//System.Diagnostics.Debug.WriteLine("Ending queryProcess for dataset: " + i + " on thread: " + Thread.CurrentThread.ManagedThreadId);
}