我在C#中創建一個代碼,其中我從Access數據庫中提取一些記錄,但我需要一段時間去下一個迭代以取決於點擊一個按鈕。我嘗試了一些線程或任務,但它沒有工作,因爲它阻止了我需要它看到和點擊的UI。C# - 使用UI功能,像一個無限循環,直到按下按鈕
下面的代碼:
bool nextClick = false ;
while (readerSelect.Read())
{
// show the correct panel
if (string.Compare(readerSelect[2].ToString(), "P1") == 0)
{
// panel with type 1
textBoxP1Text1.Text = readerSelect[3].ToString();
textBoxP1Text2.Text = readerSelect[4].ToString();
pictureBoxP1Image.ImageLocation = readerSelect[6].ToString();
}
else
{
// panel with type 2
textBoxP1Text2.Text = readerSelect[5].ToString();
}
//this while need to be kind of infinite so the interation can't be processed and
//so when i need to change iteration i click the buttonNext
while (!nextClick) {
startWhile:;
MethodInvoker mi = delegate() {
if (nextClick)
{
Application.DoEvents();
// System.Windows.Forms.Application.Run();
}
};
this.Invoke(mi);
//break;
goto startWhile;
}
private void buttonNext_Click(object sender, EventArgs e)
{
// click on the next button
nextClick = true;
}
如果您只需點擊一下即可轉到下一個項目,請不要使用while循環...只需在每次點擊時執行一次... – Milney
這將對我們的連接產生重大影響,差,但謝謝你回答 –
你是什麼意思'對你的連接有重大影響'?這只是不正確的。你現在和現在這樣做的方式只會讀取有多少記錄。你誤會了。我建議閱讀DataReader文檔,直到你更好地理解它作爲第一點 – Milney