Sudhir Goswami
Sudhir Goswami

Reputation: 65

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.

Upvotes: 0

Views: 1814

Answers (4)

Adarsh Shah
Adarsh Shah

Reputation: 6775

You can use Thread.Sleep(2000)

Upvotes: 3

Nagaraj S
Nagaraj S

Reputation: 13474

You can use 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

// 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;

Upvotes: 3

venerik
venerik

Reputation: 5904

You should use the await statement in the call to the service. 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

Upvotes: 1

Sagar Rawal
Sagar Rawal

Reputation: 1442

Using Thread.sleep method to call after 2 second call next statement.

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

Upvotes: 0

Related Questions