Zgrkpnr
Zgrkpnr

Reputation: 1231

The second iteration of my loop displays the values of a collection in a different order than the first one

Related part of the code is here:

 private async void Start_Button_Click(object sender, EventArgs e)
{

    try
    {

    timer1.Interval = 50;
    timer1.Enabled = true;
    timer1.Start();
    LoopCheck = true;
    trackBar1.Hide();
    label1.Hide();
    label2.Hide();
    label3.Hide();
    label4.Hide();
    label5.Hide();
    label6.Hide();
    label7.Hide();
    label8.Hide();
    label9.Hide();
    label10.Hide();
    label11.Hide();
    textBox1.Font = new System.Drawing.Font("Times New Roman", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
        while (LoopCheck)
        {
            foreach (string word in WordsOfFile)
            {

                WordToShow = word;
                await Task.Delay(time);

            }
        }
    }
    catch
    {
        System.Windows.Forms.MessageBox.Show("You need to add text by clicking 'Load Text' button", "Error!");

        textBox1.Font = new System.Drawing.Font("Times New Roman", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
        LoopCheck = false;
        timer1.Stop();
        trackBar1.Show();
        label1.Show();
        label2.Show();
        label3.Show();
        label4.Show();
        label5.Show();
        label6.Show();
        label7.Show();
        label8.Show();
        label9.Show();
        label10.Show();
        label11.Show();
    }

}


private void Stop_Button_Click(object sender, EventArgs e)
{


    LoopCheck = false;
    timer1.Stop();
    trackBar1.Show();
    label1.Show();
    label2.Show();
    label3.Show();
    label4.Show();
    label5.Show();
    label6.Show();
    label7.Show();
    label8.Show();
    label9.Show();
    label10.Show();
    label11.Show();

}

Outside of this code I am showing this WordToShow string in a textbox. When I click StartButton this block of code starts. When I click StopButton this LoopCheck value is set to false. That means the loop is stopped. It works well on the first click to start.

After I click on start again the words in WordsOfFile are not shown in the correct order. That means I am missing something about the foreach block.

What could it be?

Upvotes: 0

Views: 87

Answers (2)

user3529273
user3529273

Reputation:

What await Task.Delay() does is creating delay tasks in every iteration. So even you change LoppCheck value from true to false you still have the tasks that were created. That is why you can see words that are not in the correct order. And also I can say you see words change faster than you assigned. That is because awaited delays are still on work. So you need to cancel that tasks.

here is the documentation of MSDN about cancelling tasks.

Upvotes: 1

Dimitri
Dimitri

Reputation: 7013

await keyword tells the program to iterate through the foreach and come back when Task.Delay is complete. but since Task.Delay is not exact which thread comes back from await can't be controlled. And you get your results out of order.

You'll have to implement ordering.

Depending on the goal you're trying to achieve, there might be better options out there.

EDIT:

If all you're trying to do is have a responsive UI, look into BackgroundWorker class. It may seem complicated at first but its not.

Upvotes: 1

Related Questions