每次循环更新变量
本文关键字:变量 更新 循环 | 更新日期: 2023-09-27 18:16:34
编写了这段代码,一旦点击按钮就调用Async函数。
但是我希望它在每次循环中不断更新Page变量。
示例:第一个循环页= 1第二个循环页=2,以此类推。点击另一个按钮停止循环。
private void button2_Click(object sender, EventArgs e)
{
var searchParameters = new PlayerSearchParameters
{
Page = 2,
League = League.BarclaysPremierLeague,
Nation = Nation.England,
Position = Position.CentralForward,
Team = Team.ManchesterUnited
};
LoginM.SearchItemsAsync(searchParameters);
}
很抱歉没有给出详细的解释searchparameters从文本框中获取,并通过调用
发送给LoginM.cs文件上的Async函数:LoginM.SearchItemsAsync(searchParameters);
我想要loginm . searchemsasync (searchParameters);连续调用,每次调用Page变量时,它的值应该加1
如何创建一个循环来完成上面的操作?
是一个WinForm项目
您需要一个位置来保存当前值,在本例中,我将其设置为名为pageNum
的本地值。在你的循环中,你将增加这个变量,并将它赋值给初始化器中构造的PlayerSearchParameters
对象的Page
属性。
如果您希望方法在开始下一次循环之前等待异步方法完成,则将事件处理程序async
和await
设置为您正在调用的事件处理程序的结果。
private async void button2_Click(object sender, EventArgs e)
{
var pageNum = 0;
while(true) //<-- Insert whatever your real condition is here instead of true
{
var searchParameters = new PlayerSearchParameters
{
Page = ++pageNum,
League = League.BarclaysPremierLeague,
Nation = Nation.England,
Position = Position.CentralForward,
Team = Team.ManchesterUnited
};
await LoginM.SearchItemsAsync(searchParameters);
}
}
如果你想获得这种行为,而不必先点击一个按钮,你可以把方法的主体在Load
事件的处理程序在你的窗体