簡體   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())
                        ));
    }
}

上面的代碼正在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM