1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class Command
{
/* ... */
}
private async void button1_Click(object sender, EventArgs e)
{
List<Command> myCommandList = new List<Command>();
for (int i = 0; i < 10; i++) myCommandList.Add(new Command());
button1.Enabled = false;
await DoWork(myCommandList);
button1.Enabled = true;
}
private async Task DoWork(List<Command> commandList)
{
IProgress<int> progress = new Progress<int>((i) => progressBar1.Value = (int)(progressBar1.Maximum * i / commandList.Count));
await Task.Run(() =>
{
int i = 0;
foreach(Command c in commandList)
{
ValidateCommand(c);
progress.Report(++i);
}
});
}
private void ValidateCommand(Command myCommand)
{
// Faire les calculs avec myCommand...
Thread.Sleep(1000); // Pour simuler
} |
Partager