How to execute next loop after specific time?
In a foreach loop, how to execute the 2nd loop after 5 seconds of 1st loop, and then execute 3rd loop after 3 seconds of the 2nd loop?
1st loop
after 5 second
2nd loop
after 3 second
3rd loop
As I am doing Webbrowser automation in windows form, there are numerous of link to be clicked automatically, after the link is clicked , it will automatically insert a text into a textbox and click on a submit button. The process:
foreach (HtmlElement ahref in ahrefs)
{
ahref.InvokeMember("click");
Application.DoEvents();
txtboxPrice.SetAttribute("value", "100");
btnSubmit.InvokeMember("click");
}
The problem of what i facing now is, the loop is too fast until the Webbrowser can't finish implement txtboxprice.setattribute and btnsubmit.invokemember, it already clicked on next loop's ahref.
Therefore, i need to trigger time control in order to make sure txtboxprice.setattribute and btnsubmit.invokemember is implemented before开发者_如何学C proceed to next looping.
using System.Threading;
foreach(..) { } // your for or foreach or any code you want
Thread.Sleep(5000); // pause the current thread on x milliseconds
foreach(..) { }
Thread.Sleep(3000);
foreach(..) { }
Thread.Sleep method blocks the current thread for the specified number of milliseconds.
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Sleep for 2 seconds.");
Thread.Sleep(2000);
}
You could do
for(int i=0;i<number_of_times;i++)
{
....
if(i == number_of_times - 1)
Thread.Sleep(5000);//time in millsecond, namespace System.Threading
}
for(int i=0;i<number_of_times;i++)
{
....
if(i == number_of_times - 1)
Thread.Sleep(3000);
}
for(int i=0;i<number_of_times;i++)
{
....
}
But I wonder why you want to do such a thing.
精彩评论