我有一个循环运行的方法可能需要很长时间才能完成,因为它需要从API中获取数据。 我想要做的是在前端显示一条消息,说明系统在每个循环中的进展情况。有没有办法在处理时更新前端?
public static void GetScreenshot(List<string> urlList, List<DesiredCapabilities> capabilities, String platform, Literal updateNote)
{
foreach (String url in urlList)
{
String siteName = new Uri(url).Host;
String dir = AppDomain.CurrentDomain.BaseDirectory+ "/Screenshots/" + siteName + "/" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm");
foreach (DesiredCapabilities cap in capabilities)
{
String saveDirectory = "";
if (platform == "btnGenDesktopScreens")
{
saveDirectory = dir + "/" + cap.GetCapability("os") + "-" + cap.GetCapability("os_version") + "-" + cap.GetCapability("browser") + cap.GetCapability("browser_version");
}
else if(platform == "btnMobile")
{
saveDirectory = dir + "/" + cap.GetCapability("platform") + "" + cap.GetCapability("device") + "-" + cap.GetCapability("browserName");
}
updateNote.Text += "<br/>" + cap.GetCapability("platform") + " - " + cap.GetCapability("device") + "-" + cap.GetCapability("browserName");
//I'd like to display a message here
TakeScreenshot(url, cap, saveDirectory);
//I'd like to display a message here
}
}
}
有没有人遇到过这样做的方法?
答案 0 :(得分:0)
根据您将反馈返回给用户的方式,您可以通过在循环中使用HttpResponse.Flush将一部分HTML响应一次推送给用户来完成此操作。见https://msdn.microsoft.com/en-us/library/system.web.httpresponse.flush(v=vs.100).aspx