我有一個winform應用程序,它顯示了一些信息及時,每次加載數據時,我設置了7秒的延遲時間,像這樣:System.Threading.Thread.Sleep(7000),因此可以查看信息。我想要有一個按鈕,可以讓我無需等待即可跳到下一個信息。刪除延遲時間:System.Threading.Thread.Sleep使用c#(包括代碼)
我使用的邏輯如下:獲取信息,如果有的話,等待7秒,接下來的數據等等。所以,如果我按下按鈕,我想將時間設置爲0.
有什麼方法可以取消等待時間?
這裏是代碼:
ManualResetEvent wait_handle = new ManualResetEvent(true);
{...}
private void TheLoop(object stateinfo)
{
bool hasInfo = true;
bool hasLines = GetLinesOnProduction();
while (doLoop)
{
wait_handle.WaitOne();
if (hasLines)
{
param1 = Lines[CurrentLine].line;
param2 = Lines[CurrentLine].WO;
//Here I query the DB for the CurrentLine Data
ShowLineInformation(CurrentLine);
ShowChartByHour(param1, param2, out hasInfo);
if (hasInfo)
System.Threading.Thread.Sleep(7000);
//Here I move to the next line
if (CurrentLine < Lines.Count - 1)
CurrentLine++;
else
{
CurrentLine = 0; //Start all over again
hasLines = GetLinesOnProduction();
}
}
else
{
System.Threading.Thread.Sleep(40000); //(No Lines)Wait to query for lines again
hasLines = GetLinesOnProduction();
}
}
}
private void btnPauseResume_Click(object sender, EventArgs e)
{
if (btnPauseResume.Text == "Pause")
{
btnPauseResume.Text = "Resume";
wait_handle.Reset();
}
else
{
btnPauseResume.Text = "Pause";
wait_handle.Set();
}
}
請告訴我們你的代碼。你應該使用多個按鈕。 – SLaks
'wait_handle.WaitOne();'只等待wait_handle爲Set()。您必須指定等待的毫秒數量,在這種情況下爲'WaitOne(7000)'。 – CodeCaster
@CodeCaster,但我使用該WaitOne();暫停/恢復線程,我不希望在這一點上有任何等待時間。據我所知,我必須把它放在我想要做這些操作的地方。 – Somebody