Как создать свою собственную функцию для асинхронной операции в c ++ UWP? - PullRequest
0 голосов
/ 11 декабря 2018

С this , Все примеры используют собственную функцию Windows для асинхронной операции.
Не возможно создать собственную асинхронную функцию?

Например

//MainPage.xaml.h
int summation(int start_num, int stop_num);

//MainPage.xaml.cpp
int test_thread::MainPage::summation(int start_num, int stop_num)
{
    int sum = 0;
    for (start_num; start_num <= stop_num; start_num++) sum += start_num;
    return sum;
}

И назовите это так,

IAsyncOperation<int> sum = summation(1, 10000000000);//error
auto sumTask = Concurrency::create_task(sum);
sumTask.then(/*...*/);

Я также пытался определить функцию как

//MainPage.xaml.h
IAsyncOperation<int>^ summer(int start_num, int stop_num);

Но получил эту ошибку не может перегрузить функции, отличающиеся возвратомодин тип

1 Ответ

0 голосов
/ 12 декабря 2018

Это

    int a = 1;
    int b = 2;

    auto workItem = ref new Windows::System::Threading::WorkItemHandler([this, a, b](IAsyncAction^ workItem)
    {
        Platform::String ^ updateString = (a+b).ToString();

        //UI update
        Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
            Windows::UI::Core::CoreDispatcherPriority::High,
            ref new Windows::UI::Core::DispatchedHandler([this, updateString]()
        {
            textBlock->Text = updateString;
        }));
        //END UI upate
    });

    auto asyncAction = Windows::System::Threading::ThreadPool::RunAsync(workItem);

Или это

    int inputPrime = platformSting2Int(textBox_primeCheck->Text);
    std::shared_ptr<bool> isItPrime = std::make_shared<bool>(true);

    auto workItem = ref new Windows::System::Threading::WorkItemHandler([this, inputPrime, isItPrime](IAsyncAction^ workItem)
    {
        *isItPrime = IsPrime(inputPrime);//IsPrime() is my custom function
    });

    auto asyncAction = Windows::System::Threading::ThreadPool::RunAsync(workItem);

    asyncAction->Completed = ref new Windows::Foundation::AsyncActionCompletedHandler([this, isItPrime](IAsyncAction^ asyncInfo, AsyncStatus asyncStatus)
    {
        if (asyncStatus == AsyncStatus::Canceled) return;

        Platform::String ^ updateString = "is not prime.";
        if(*isItPrime) updateString = "is prime.";

        //UI update
        Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
            Windows::UI::Core::CoreDispatcherPriority::High,
            ref new Windows::UI::Core::DispatchedHandler([this, updateString]()
        {
            textBlock_primeCheckResult->Text = updateString;
        }));
        //END UI upate
    });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...