简体   繁体   English

2秒后调用方法

[英]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())
                        ));
    }
}

Above code is making sample data in for loop but I will get this result from some service which I need to call after 2 second, but it will execute only one time once he get response it will never execute. 上面的代码正在for loop制作示例数据,但是我将从需要在2秒后调用的某些服务中获得此结果,但是一旦他获得response ,它将仅执行一次,它将永远不会执行。

您可以使用Thread.Sleep(2000)

You can use Timer Class 您可以使用Timer Class

aTimer = new System.Timers.Timer(1000 * 60 * 2);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Enabled = true; 

or You can use Timer.Elapsed Event 或者您可以使用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;

You should use the await statement in the call to the service. 您应该在对服务的调用中使用await语句。 This way your program will wait until the service responds: 这样,您的程序将等待直到服务响应:

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

Have a look here for asynchronous programming: http://msdn.microsoft.com/en-us/library/hh191443.aspx 在这里查看异步编程: http : //msdn.microsoft.com/zh-cn/library/hh191443.aspx

Using Thread.sleep method to call after 2 second call next statement. 使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM