User guide

Table Of Contents
describeRequest.SpotInstanceRequestId.Add(spotInstanceRequest.SpotIn
stanceRequestId);
10 }
1 // Create a variable that will track whether there are any
// requests still in the open state.
bool anyOpen;
5 // Create a list to store any instances that were activated.
List<String> instanceIds = new List<String>();
do
{
10 // Initialize the anyOpen variable to false, which assumes there
// are no requests open unless we find one that is still open.
anyOpen = false;
instanceIds.Clear();
15 try
{
// Retrieve all of the requests we want to monitor.
DescribeSpotInstanceRequestsResponse describeResponse = ec2.De
scribeSpotInstanceRequests(describeRequest);
20 // Look through each request and determine if they are all in
// the active state.
foreach (SpotInstanceRequest spotInstanceRequest in de
scribeResponse.DescribeSpotInstanceRequestsResult.SpotInstanceRequest)
{
// If the state is open, it hasn't changed since we attempted
25 // to request it. There is the potential for it to transition
// almost immediately to closed or canceled, so we compare
// against open instead of active.
if (spotInstanceRequest.State.Equals("open", StringComparis
on.InvariantCulture))
{
30 anyOpen = true;
break;
}
else if (spotInstanceRequest.State.Equals("active", StringCom
parison.InvariantCulture))
{
35 // Add the instance id to the list we will
// eventually terminate.
instanceIds.Add(spotInstanceRequest.InstanceId);
}
}
40 }
catch (AmazonEC2Exception e)
{
// If we have an exception, ensure we don't break out of
// the loop. This prevents the scenario where there was
45 // blip on the wire.
anyOpen = true;
Console.WriteLine(e.Message);
}
Version v2.0.0
56
AWS SDK for .NET Developer Guide
Step 4: Determining the State of Your Spot Request