到目前为止,我有此代码:
System.Threading.Thread.Sleep(2000);
(p.CommonWord,
p.JishoJlpt,
p.JishoWanikani,
p.JishoExists,
p.English,
p.Furigana,
p.JmdictId,
p.JmdictHiragana,
p.Kanji) = GetAll(block, 1);
if (p.Kanji == "kanj Id not found")
{
System.Threading.Thread.Sleep(5000);
(p.CommonWord,
p.JishoJlpt,
p.JishoWanikani,
p.JishoExists,
p.English,
p.Furigana,
p.JmdictId,
p.JmdictHiragana,
p.Kanji) = GetAll(block, 2);
}
if (p.Kanji == "kanj Id not found")
{
System.Threading.Thread.Sleep(10000);
(p.CommonWord,
p.JishoJlpt,
p.JishoWanikani,
p.JishoExists,
p.English,
p.Furigana,
p.JmdictId,
p.JmdictHiragana,
p.Kanji) = GetAll(block, 3);
}
if (p.Kanji == "kanj Id not found")
{
System.Threading.Thread.Sleep(20000);
(p.CommonWord,
p.JishoJlpt,
p.JishoWanikani,
p.JishoExists,
p.English,
p.Furigana,
p.JmdictId,
p.JmdictHiragana,
p.Kanji) = GetAll(block, 4);
}
if (p.Kanji == "kanj Id not found")
{
System.Threading.Thread.Sleep(30000);
(p.CommonWord,
p.JishoJlpt,
p.JishoWanikani,
p.JishoExists,
p.English,
p.Furigana,
p.JmdictId,
p.JmdictHiragana,
p.Kanji) = GetAll(block, 5);
}
if (p.Kanji == "kanj Id not found")
{
System.Threading.Thread.Sleep(60000);
(p.CommonWord,
p.JishoJlpt,
p.JishoWanikani,
p.JishoExists,
p.English,
p.Furigana,
p.JmdictId,
p.JmdictHiragana,
p.Kanji) = GetAll(block, 6);
}
if (p.Kanji == "kanj Id not found")
{
System.Threading.Thread.Sleep(1200000);
(p.CommonWord,
p.JishoJlpt,
p.JishoWanikani,
p.JishoExists,
p.English,
p.Furigana,
p.JmdictId,
p.JmdictHiragana,
p.Kanji) = GetAll(block, 7);
}
任何人都可以建议我如何简化此操作,并且仍然增加每次运行之间的时间。我想要的是从5秒的延迟开始,并在每次重复7次之后将其加倍,直到结束7次,即使p.Kanji在这7次尝试之后“未找到kanj Id”。
答案 0 :(得分:2)
// start with 5 seconds
int sleepTime = 5000;
for (int i = 0; i < 7; i++)
{
// do sleep
System.Threading.Thread.Sleep(sleepTime);
// double sleep time
sleepTime *= 2;
// execute
(p.CommonWord,
p.JishoJlpt,
p.JishoWanikani,
p.JishoExists,
p.English,
p.Furigana,
p.JmdictId,
p.JmdictHiragana,
p.Kanji) = GetAll(block, i+1);
// exit if necessary
if (p.Kanji != "kanj Id not found")
break;
}
答案 1 :(得分:1)
首先,将睡眠时间设置为变量,也许是private int SleepTime = 5000
。
然后将if
更改为while
,并在调用GetAll
函数之后,设置SleepTime *= 2
或类似的东西,以增加等待下一个循环的时间。>
如果SleepTime
超出一定数量(放弃),则增加SleepTime
退出循环后,您可能还希望在循环的底部放置退出条件