edit-icon download-icon

Asynchronous call

Last Updated: Feb 28, 2018

Alibaba Cloud C++ SDK supports asynchronous operations. Make an asynchronous call in the format of action+Ayscn, such as describeInstancesAsync.

The following is example of an asynchronous operation that calls the DescribeInstances to obtain the detailed information of all ECS instances in the specified region.

Note: You must replace the values of your-region-id, your-access-key-id, and your-access-key-secret in the example.

  1. #include <chrono>
  2. #include <iostream>
  3. #include <alibabacloud/core/AlibabaCloud.h>
  4. #include <alibabacloud/ecs/EcsClient.h>
  5. using namespace AlibabaCloud;
  6. using namespace AlibabaCloud::Ecs;
  7. int main(int argc, char** argv)
  8. {
  9. // Initialize the SDK
  10. AlibabaCloud::InitializeSdk();
  11. // Configure the ecs instance
  12. ClientConfiguration configuration("<your-region-id>");
  13. EcsClient client("<your-access-key-id>", "<your-access-key-secret>", configuration);
  14. // Create a request and set parameters
  15. Model::DescribeInstancesRequest request;
  16. request.setPageSize(10);
  17. client.describeInstancesAsync(request,
  18. [](const EcsClient*, const Model::DescribeInstancesRequest&,
  19. const EcsClient::DescribeInstancesOutcome&outcome,
  20. const std::shared_ptr<const AsyncCallerContext>&)
  21. {
  22. if (!outcome.isSuccess())
  23. // Handle exceptions
  24. std::cout << outcome.error().errorCode() << std::endl;
  25. else
  26. std::cout << "totalCount: " << outcome.result().getTotalCount() << std::endl;
  27. }, nullptr);
  28. // Execute other operations
  29. using namespace std::chrono_literals;
  30. std::this_thread::sleep_for(5s);
  31. // Close the SDK
  32. AlibabaCloud::ShutdownSdk();
  33. return 0;
  34. }
Thank you! We've received your feedback.