我在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