[英]Method Call after 2 seconds
HttpResponseMessage response = client.PostAsJsonAsync(ConfigurationManager.AppSettings[Constants.BidApiBaseURL], objClientBidRequest).Result;
if (response.StatusCode.ToString() == "OK")
{
// Send request after 2 second for bid result
string bidContent = "<iframe src=maps.google.com?gps=....></iframe>";
for (int i = 1; i <= 4; i++)
{
lstExpertBidResponse.Add(
new BidResponse(
objClientBidRequest.RequestId.ToString(),
bidContent,
i.ToString(),
"W" + i.ToString(),
GetFeedBackScore("W" + i.ToString()),
GetExpertID("W" + i.ToString())
));
}
}
上面的代码正在for loop
制作示例数据,但是我将从需要在2秒后调用的某些服务中获得此结果,但是一旦他获得response
,它将仅执行一次,它将永远不会执行。
您可以使用Thread.Sleep(2000)
您可以使用Timer Class
aTimer = new System.Timers.Timer(1000 * 60 * 2);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true;
或者您可以使用Timer.Elapsed Event
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
您应该在对服务的调用中使用await
语句。 这样,您的程序将等待直到服务响应:
var task = await client.PostAsJsonAsync(
ConfigurationManager.AppSettings[Constants.BidApiBaseURL],
objClientBidRequest);
// wait for service to complete
HttpResponseMessage response = task.result;
if (response.StatusCode.ToString() == "OK")
// etc etc
在这里查看异步编程: http : //msdn.microsoft.com/zh-cn/library/hh191443.aspx
使用Thread.sleep方法在2秒后调用next语句进行调用。
HttpResponseMessage response = client.PostAsJsonAsync(ConfigurationManager.AppSettings[Constants.BidApiBaseURL], objClientBidRequest).Result;
if (response.StatusCode.ToString() == "OK")
{
// Send request after 2 second for bid result
Thread.Sleep(2000);
string bidContent = "<iframe src=maps.google.com?gps=....></iframe>";
for (int i = 1; i <= 4; i++)
{
lstExpertBidResponse.Add(
new BidResponse(
objClientBidRequest.RequestId.ToString(),
bidContent,
i.ToString(),
"W" + i.ToString(),
GetFeedBackScore("W" + i.ToString()),
GetExpertID("W" + i.ToString())
));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.