다음을 통해 공유


방법: WRL을 사용하여 비동기 작업 완료

이 문서에 사용 하는 방법을 보여 줍니다 있는 Windows 런타임 C++ 템플릿 라이브러리 (WRL) 비동기 작업을 시작 하 고 작업을 완료 하면 작업을 수행 합니다.

이 문서의 두 가지 예제를 보여 줍니다.첫 번째 예제는 비동기 타이머를 시작 및 타이머 만료 될 때까지 대기 합니다.이 예제에서는 타이머 개체를 만들 때 비동기 작업 지정.두 번째 예제에서는 백그라운드 작업자 스레드를 실행합니다.작업 하는 방법을 보여 주는이 예제는 Windows 런타임 를 반환 하는 메서드는 IAsyncInfo 인터페이스.콜백 함수는 비동기 작업의 결과 처리 하는 이벤트 처리기를 지정 하 여 사용할 수 있으므로 두 예제 모두에서 중요 한 부분입니다.

해당 구성 요소의 인스턴스를 만들고 속성 값을 검색 하는 기본적인 예제를 보려면 방법: WRL을 사용하여 Windows 런타임 구성 요소 활성화 및 사용.

팁

이러한 예제 람다 식을 사용 하는 콜백을 정의 합니다.함수 포인터, 함수 개체 (functors)를 사용할 수도 있습니다 또는 std::function 개체입니다.C + + 람다 식에 대 한 자세한 내용은 C + +에서 람다 식.

예제: 타이머를 작동

다음 단계는 비동기 타이머를 시작 하 고 타이머 만료 될 때까지 기다립니다.다음은 완성된 예제입니다.

주의 정보주의

일반적으로 사용 되지만 WRL 에 있는 Windows 스토어 응용 프로그램,이 예제 그림을 사용 하 여 콘솔 응용 프로그램.같은 함수 wprintf_s 에서 사용할 수 없습니다는 Windows 스토어 응용 프로그램입니다.형식에 사용할 수 있는 함수에 대 한 자세한 내용은 Windows 스토어 응용 프로그램을 참조 하십시오 CRT 함수가 /zw에 지원 되지 않습니다Win32 및 Windows 저장소에 대 한 COM 응용 프로그램.

  1. 포함 (#include)은 필요한 Windows 런타임, WRL, 또는 표준 c + + 라이브러리 헤더입니다.

    #include <Windows.Foundation.h>
    #include <Windows.System.Threading.h>
    #include <wrl/event.h>
    #include <stdio.h>
    #include <Objbase.h>
    
    using namespace ABI::Windows::Foundation;
    using namespace ABI::Windows::System::Threading;
    using namespace Microsoft::WRL;
    using namespace Microsoft::WRL::Wrappers;
    

    Windows.system.threading.h는 비동기 타이머를 사용 하는 데 필요한 형식을 선언 합니다.

    활용 하는 것이 좋습니다 있는 using namespace 지시문.cpp 파일에 코드를 쉽게 읽을 수 있도록 합니다.

  2. 초기화는 Windows 런타임.

    // Initialize the Windows Runtime.
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
    if (FAILED(initialize))
    {
        return PrintError(__LINE__, initialize);
    }
    
  3. 만들기는 정품 인증 공장에 있는 ABI::Windows::System::Threading::IThreadPoolTimer 인터페이스.

    // Get the activation factory for the IThreadPoolTimer interface.
    ComPtr<IThreadPoolTimerStatics> timerFactory;
    HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_System_Threading_ThreadPoolTimer).Get(), &timerFactory);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }
    

    Windows 런타임 정규화 된 이름을 사용 하 여 형식을 식별 합니다.RuntimeClass_Windows_System_Threading_ThreadPoolTimer 매개 변수에서 제공 하는 문자열입니다를 Windows 런타임 및 필요한 런타임 클래스 이름을 포함 합니다.

  4. 만들기는 이벤트 주 응용 프로그램 타이머 콜백에 동기화 개체입니다.

    // Create an event that is set after the timer callback completes. We later use this event to wait for the timer to complete. 
    // This event is for demonstration only in a console app. In most apps, you typically don't wait for async operations to complete.
    Event timerCompleted(CreateEventEx(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, WRITE_OWNER | EVENT_ALL_ACCESS));
    hr = timerCompleted.IsValid() ? S_OK : HRESULT_FROM_WIN32(GetLastError());
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }
    

    [!참고]

    이 이벤트는 콘솔 응용 프로그램의 일부로 데모입니다.이벤트를 사용 하 여 비동기 작업 응용 프로그램 종료 하기 전에 완료 하도록 하는 예제입니다.일반적으로 대부분의 응용 프로그램에 비동기 작업이 완료 기다려야 하지 마십시오.

  5. 만들기는 IThreadPoolTimer 2 초 후 만료 되는 개체입니다.사용 된 Callback 이벤트 처리기를 만드는 기능 (는 ABI::Windows::System::Threading::ITimerElapsedHandler 개체).

    // Create a timer that prints a message after 2 seconds.
    
    TimeSpan delay;
    delay.Duration = 20000000; // 2 seconds.
    
    auto callback = Callback<ITimerElapsedHandler>([&timerCompleted](IThreadPoolTimer* timer) -> HRESULT
    {
        wprintf_s(L"Timer fired.\n");
    
        TimeSpan delay;
        HRESULT hr = timer->get_Delay(&delay);
        if (SUCCEEDED(hr))
        {
            wprintf_s(L"Timer duration: %2.2f seconds.\n", delay.Duration / 10000000.0);
        }
    
        // Set the completion event and return.
        SetEvent(timerCompleted.Get());
        return hr;
    });
    hr = callback ? S_OK : E_OUTOFMEMORY;
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }
    
    ComPtr<IThreadPoolTimer> timer;
    hr = timerFactory->CreateTimer(callback.Get(), delay, &timer);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }
    
  6. 메시지를 콘솔에 인쇄 하 고 타이머 콜백이 완료 될 때까지 기다립니다.모든 ComPtr 및 RAII 개체 범위를 두고 자동으로 해제 됩니다.

    // Print a message and wait for the timer callback to complete.
    wprintf_s(L"Timer started.\nWaiting for timer...\n");
    
    // Wait for the timer to complete.
    WaitForSingleObjectEx(timerCompleted.Get(), INFINITE, FALSE);
    // All smart pointers and RAII objects go out of scope here.
    

전체 예제는 다음과 같습니다.

// wrl-consume-async.cpp
// compile with: runtimeobject.lib
#include <Windows.Foundation.h>
#include <Windows.System.Threading.h>
#include <wrl/event.h>
#include <stdio.h>
#include <Objbase.h>

using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::System::Threading;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

// Prints an error string for the provided source code line and HRESULT
// value and returns the HRESULT value as an int.
int PrintError(unsigned int line, HRESULT hr)
{
    wprintf_s(L"ERROR: Line:%d HRESULT: 0x%X\n", line, hr);
    return hr;
}

int wmain()
{
    // Initialize the Windows Runtime.
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
    if (FAILED(initialize))
    {
        return PrintError(__LINE__, initialize);
    }

    // Get the activation factory for the IThreadPoolTimer interface.
    ComPtr<IThreadPoolTimerStatics> timerFactory;
    HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_System_Threading_ThreadPoolTimer).Get(), &timerFactory);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    // Create an event that is set after the timer callback completes. We later use this event to wait for the timer to complete. 
    // This event is for demonstration only in a console app. In most apps, you typically don't wait for async operations to complete.
    Event timerCompleted(CreateEventEx(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, WRITE_OWNER | EVENT_ALL_ACCESS));
    hr = timerCompleted.IsValid() ? S_OK : HRESULT_FROM_WIN32(GetLastError());
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    // Create a timer that prints a message after 2 seconds.

    TimeSpan delay;
    delay.Duration = 20000000; // 2 seconds.

    auto callback = Callback<ITimerElapsedHandler>([&timerCompleted](IThreadPoolTimer* timer) -> HRESULT
    {
        wprintf_s(L"Timer fired.\n");

        TimeSpan delay;
        HRESULT hr = timer->get_Delay(&delay);
        if (SUCCEEDED(hr))
        {
            wprintf_s(L"Timer duration: %2.2f seconds.\n", delay.Duration / 10000000.0);
        }

        // Set the completion event and return.
        SetEvent(timerCompleted.Get());
        return hr;
    });
    hr = callback ? S_OK : E_OUTOFMEMORY;
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    ComPtr<IThreadPoolTimer> timer;
    hr = timerFactory->CreateTimer(callback.Get(), delay, &timer);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    // Print a message and wait for the timer callback to complete.
    wprintf_s(L"Timer started.\nWaiting for timer...\n");

    // Wait for the timer to complete.
    WaitForSingleObjectEx(timerCompleted.Get(), INFINITE, FALSE);
    // All smart pointers and RAII objects go out of scope here.
}
/*
Output:
Timer started.
Waiting for timer...
Timer fired.
Timer duration: 2.00 seconds.
*/

Hh973451.collapse_all(ko-kr,VS.110).gif코드 컴파일

코드를 컴파일하려면 복사한 다음 Visual Studio 프로젝트에 붙여 넣을 하거나 라는 파일에 붙여 넣을 wrl 소비 async.cpp 및 다음 Visual Studio 명령 프롬프트 창에서 다음 명령을 실행 합니다.

cl.exe wrl-consume-async.cpp runtimeobject.lib

예: 백그라운드 스레드를 작업

다음 단계 작업자 스레드를 시작 하 고 해당 스레드에 의해 수행 되는 동작을 정의 합니다.다음은 완성된 예제입니다.

팁

작업 하는 방법을 보여 주는이 예제는 ABI::Windows::Foundation::IAsyncAction 인터페이스.이 패턴을 구현 하는 인터페이스에 적용할 수 있습니다 IAsyncInfo: IAsyncAction, IAsyncActionWithProgress, IAsyncOperation, 및 IAsyncOperationWithProgress.

  1. 포함 (#include)은 필요한 Windows 런타임, WRL, 또는 표준 c + + 라이브러리 헤더입니다.

    #include <Windows.Foundation.h>
    #include <Windows.System.Threading.h>
    #include <wrl/event.h>
    #include <stdio.h>
    #include <Objbase.h>
    
    using namespace ABI::Windows::Foundation;
    using namespace ABI::Windows::System::Threading;
    using namespace Microsoft::WRL;
    using namespace Microsoft::WRL::Wrappers;
    

    Windows.system.threading.h는 작업자 스레드를 사용 하는 데 필요한 형식을 선언 합니다.

    활용 하는 것이 좋습니다 있는 using namespace 지시문.cpp 파일에 코드를 쉽게 읽을 수 있도록 합니다.

  2. 초기화는 Windows 런타임.

    // Initialize the Windows Runtime.
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
    if (FAILED(initialize))
    {
        return PrintError(__LINE__, initialize);
    }
    
  3. 만들기는 정품 인증 공장에 있는 ABI::Windows::System::Threading::IThreadPoolStatics 인터페이스.

    // Get the activation factory for the IThreadPoolStatics interface.
    ComPtr<IThreadPoolStatics> threadPool;
    HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_System_Threading_ThreadPool).Get(), &threadPool);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }
    
  4. 만들기는 이벤트 완료 작업자 스레드는 주 응용 프로그램에 동기화 개체입니다.

        // Create an event that is set after the timer callback completes. We later use this event to wait for the timer to complete. 
        // This event is for demonstration only in a console app. In most apps, you typically don't wait for async operations to complete.
        Event threadCompleted(CreateEventEx(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, WRITE_OWNER | EVENT_ALL_ACCESS));
        hr = threadCompleted.IsValid() ? S_OK : HRESULT_FROM_WIN32(GetLastError());
        if (FAILED(hr))
        {
            return PrintError(__LINE__, hr);
        }
    
    

    [!참고]

    이 이벤트는 콘솔 응용 프로그램의 일부로 데모입니다.이벤트를 사용 하 여 비동기 작업 응용 프로그램 종료 하기 전에 완료 하도록 하는 예제입니다.일반적으로 대부분의 응용 프로그램에 비동기 작업이 완료 기다려야 하지 마십시오.

  5. 호출을 IThreadPoolStatics::RunAsync 메서드는 작업자 스레드를 만들 수 있습니다.사용은 Callback 함수는 동작을 정의할 수 있습니다.

    wprintf_s(L"Starting thread...\n");
    
    // Create a thread that computes prime numbers.
    ComPtr<IAsyncAction> asyncAction;
    hr = threadPool->RunAsync(Callback<IWorkItemHandler>([&threadCompleted](IAsyncAction* asyncAction) -> HRESULT
    {
        // Print a message.
        const unsigned int start = 0;
        const unsigned int end = 100000;
        unsigned int primeCount = 0;
        for (int n = start; n < end; n++)
        {
            if (IsPrime(n))
            {
                primeCount++;
            }
        }
    
        wprintf_s(L"There are %u prime numbers from %u to %u.\n", primeCount, start, end);
    
        // Set the completion event and return.
        SetEvent(threadCompleted.Get());
        return S_OK;
    
    }).Get(), &asyncAction);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }
    

    IsPrime 함수 다음에 나오는 전체 예제에서 정의 됩니다.

  6. 콘솔 메시지를 표시 하 고 완료 하는 스레드에 대 한 기다립니다.모든 ComPtr 및 RAII 개체 범위를 두고 자동으로 해제 됩니다.

    // Print a message and wait for the thread to complete.
    wprintf_s(L"Waiting for thread...\n");
    
    // Wait for the thread to complete.
    WaitForSingleObjectEx(threadCompleted.Get(), INFINITE, FALSE);
    
    wprintf_s(L"Finished.\n");
    
    // All smart pointers and RAII objects go out of scope here.
    

전체 예제는 다음과 같습니다.

// wrl-consume-asyncOp.cpp
// compile with: runtimeobject.lib 
#include <Windows.Foundation.h>
#include <Windows.System.Threading.h>
#include <wrl/event.h>
#include <stdio.h>
#include <Objbase.h>

using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::System::Threading;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;

// Prints an error string for the provided source code line and HRESULT
// value and returns the HRESULT value as an int.
int PrintError(unsigned int line, HRESULT hr)
{
    wprintf_s(L"ERROR: Line:%d HRESULT: 0x%X\n", line, hr);
    return hr;
}

// Determines whether the input value is prime.
bool IsPrime(int n)
{
    if (n < 2)
    {
        return false;
    }
    for (int i = 2; i < n; ++i)
    {
        if ((n % i) == 0)
        {
            return false;
        }
    }
    return true;
}

int wmain()
{
    // Initialize the Windows Runtime.
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
    if (FAILED(initialize))
    {
        return PrintError(__LINE__, initialize);
    }

    // Get the activation factory for the IThreadPoolStatics interface.
    ComPtr<IThreadPoolStatics> threadPool;
    HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_System_Threading_ThreadPool).Get(), &threadPool);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    // Create an event that is set after the timer callback completes. We later use this event to wait for the timer to complete. 
    // This event is for demonstration only in a console app. In most apps, you typically don't wait for async operations to complete.
    Event threadCompleted(CreateEventEx(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, WRITE_OWNER | EVENT_ALL_ACCESS));
    hr = threadCompleted.IsValid() ? S_OK : HRESULT_FROM_WIN32(GetLastError());
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }


    wprintf_s(L"Starting thread...\n");

    // Create a thread that computes prime numbers.
    ComPtr<IAsyncAction> asyncAction;
    hr = threadPool->RunAsync(Callback<IWorkItemHandler>([&threadCompleted](IAsyncAction* asyncAction) -> HRESULT
    {
        // Print a message.
        const unsigned int start = 0;
        const unsigned int end = 100000;
        unsigned int primeCount = 0;
        for (int n = start; n < end; n++)
        {
            if (IsPrime(n))
            {
                primeCount++;
            }
        }

        wprintf_s(L"There are %u prime numbers from %u to %u.\n", primeCount, start, end);

        // Set the completion event and return.
        SetEvent(threadCompleted.Get());
        return S_OK;

    }).Get(), &asyncAction);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    // Print a message and wait for the thread to complete.
    wprintf_s(L"Waiting for thread...\n");

    // Wait for the thread to complete.
    WaitForSingleObjectEx(threadCompleted.Get(), INFINITE, FALSE);

    wprintf_s(L"Finished.\n");

    // All smart pointers and RAII objects go out of scope here.
}
/*
Output:
Starting thread...
Waiting for thread...
There are 9592 prime numbers from 0 to 100000.
Finished.
*/

Hh973451.collapse_all(ko-kr,VS.110).gif코드 컴파일

코드를 컴파일하려면 복사한 다음 Visual Studio 프로젝트에 붙여 넣을 하거나 라는 파일에 붙여 넣을 wrl 소비 asyncOp.cpp 및 다음 Visual Studio 명령 프롬프트 창에서 다음 명령을 실행 합니다.

cl.exe wrl-consume-asyncOp.cpp runtimeobject.lib

참고 항목

개념

Windows 런타임 C++ 템플릿 라이브러리(WRL)