When I click on btnStart
, the loop is started and values are added to listBox1
. I want to pause the execution when I click on btnPause
, and again continue when click on btnStart
. How do I achieve this? Please help me. Below code I have tried but no luck.
CancellationTokenSource source = new CancellationTokenSource();
private void btnStart_Click(object sender, EventArgs e)
{
//here I want to start/continue the execution
StartLoop();
}
private async void StartLoop()
{
for(int i = 0; i < 999999; i++)
{
await Task.Delay(1000, source.Token);
listBox1.Items.Add("Current value of i = " + i);
listBox1.Refresh();
}
}
private void btnPause_Click(object sender, EventArgs e)
{
//here I want to pause/stop the execution
source.Cancel();
}