xtd 0.2.0
Loading...
Searching...
No Matches

◆ async_result

Represents the status of an asynchronous operation.

Namespace
xtd
Library
xtd.core
Examples
The following example shows how to use xtd::async_result.
#include <xtd/console>
#include <xtd/iasync_result>
#include <xtd/threading/thread>
using namespace xtd;
using namespace xtd::threading;
class async_demo {
public:
// The method to be executed asynchronously.
string test_method(int call_duration, int* thread_id) {
console::write_line("Test method begins.");
thread::sleep(call_duration);
*thread_id = thread::current_thread().managed_thread_id();
return string::format("My call time was {}.", call_duration);
}
};
// The delegate must have the same signature as the method
// it will call asynchronously.
using async_method_caller = delegate<string(int call_duration, int* thread_id)>;
auto main() -> int {
// The asynchronous method puts the thread id here.
auto thread_id = 0;
// Create an instance of the test class.
auto ad = async_demo {};
// Create the delegate.
auto caller = async_method_caller {ad, &async_demo::test_method};
// Initiate the asychronous call.
auto result = caller.begin_invoke(3000, &thread_id);
thread::sleep(0);
console::write_line("Main thread {} does some work.", thread::current_thread().managed_thread_id());
// Wait for the wait_handle to become signaled.
result->async_wait_handle().wait_one();
// Perform additional processing here.
// Call end_invoke to retrieve the results.
auto return_value = caller.end_invoke(result);
// Close the wait handle.
result->async_wait_handle().close();
console::write_line("The call executed on thread {}, with return value \"{}\".", thread_id, return_value);
}
// This code produces the following output :
//
// Main thread 1 does some work.
// Test method begins.
// The call executed on thread 2, with return value "My call time was 3000.".
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
@ thread_id
Write the thread identity, which is represented by the return value of the Thread....
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.h:43
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.h:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10