I have been developing a windows forms project where I have a 10 tasks to do, and I would like to do this in a async
way.
These tasks will star when the user click in a button and I call a async
method to do this. In my code, I already have a list of parameters for these processes.
My questions is:
A) How transform my code to run all process in parallel? (I would like to implement async/await)
B) How to provide a feedback to my UI application?
The code bellow is what I have tried:
My button to call a method to start processes
private void button1_Click(object sender, EventArgs e)
{
// almost 15 process
foreach (var process in Processes)
{
// call a async method to process
ProcessObject(process);
}
}
The method to simulate my process getting a parameter
private async void ProcessObject(ProcessViewModel process)
{
// this is my loop scope, which I need to run in parallel
{
// my code is here
// increment the progress of this process
process.Progress++;
// feedback to UI (accessing the UI controls)
UpdateRow(process);
}
}
I tried this, but I am not sure if it is the right way to update my UI (a grid).
private void UpdateRow(ProcessViewModel process)
{
dataGridView1.Rows[process.Index - 1].Cells[1].Value = process.Progress;
dataGridView1.Refresh();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…