次の方法で共有


バックグラウンド タスクの進行状況と完了を監視する

重要な API

アウトプロセスで実行されるバックグラウンド タスクによって報告された進行状況と完了をアプリで認識する方法について説明します。 (インプロセス バックグラウンド タスクの場合は、進行状況と完了を示す共有変数を設定できます)。

バックグラウンド タスクの進行状況と完了は、アプリ コードで監視できます。 これを行うために、アプリはシステムに登録したバックグラウンド タスクのイベントをサブスクライブします。

  • このトピックでは、バックグラウンド タスクを登録するアプリがあることを前提としています。 バックグラウンド タスクの作成をすぐに開始するには、「インプロセス バックグラウンド タスクの作成と登録」 を参照するか、「アウトプロセスバックグラウンド タスクを作成して登録する」。 条件とトリガーの詳細については、「バックグラウンド タスクでのアプリのサポート」を参照してください。

完了したバックグラウンド タスクを処理するイベント ハンドラーを作成する

手順 1

完了したバックグラウンド タスクを処理するイベント ハンドラー関数を作成します。 このコードは、IBackgroundTaskRegistration オブジェクトと BackgroundTaskCompletedEventArgs オブジェクトを受け取る特定のフットプリントに従う必要があります。

OnCompleted バックグラウンド タスク イベント ハンドラー メソッドには、次のフットプリントを使用します。

private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
{
    // TODO: Add code that deals with background task completion.
}
auto completed{ [this](
        Windows::ApplicationModel::Background::BackgroundTaskRegistration const& /* sender */,
        Windows::ApplicationModel::Background::BackgroundTaskCompletedEventArgs const& /* args */)
{
    // TODO: Add code that deals with background task completion.
} };
auto completed = [this](BackgroundTaskRegistration^ task, BackgroundTaskCompletedEventArgs^ args)
{
    // TODO: Add code that deals with background task completion.
};

手順 2

バックグラウンド タスクの完了を処理するコードをイベント ハンドラーに追加します。

たとえば、バックグラウンド タスクのサンプル は UI を更新します。

private void OnCompleted(IBackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs args)
{
    UpdateUI();
}
auto completed{ [this](
        Windows::ApplicationModel::Background::BackgroundTaskRegistration const& /* sender */,
        Windows::ApplicationModel::Background::BackgroundTaskCompletedEventArgs const& /* args */)
{
    UpdateUI();
} };
auto completed = [this](BackgroundTaskRegistration^ task, BackgroundTaskCompletedEventArgs^ args)
{    
    UpdateUI();
};

バックグラウンド タスクの進行状況を処理するイベント ハンドラー関数を作成する

手順 1

完了したバックグラウンド タスクを処理するイベント ハンドラー関数を作成します。 このコードは、IBackgroundTaskRegistration オブジェクトと BackgroundTaskProgressEventArgs オブジェクトを取り込む特定のフットプリントに従う必要があります。

OnProgress バックグラウンド タスク イベント ハンドラー メソッドには、次のフットプリントを使用します。

private void OnProgress(IBackgroundTaskRegistration task, BackgroundTaskProgressEventArgs args)
{
    // TODO: Add code that deals with background task progress.
}
auto progress{ [this](
    Windows::ApplicationModel::Background::BackgroundTaskRegistration const& /* sender */,
    Windows::ApplicationModel::Background::BackgroundTaskProgressEventArgs const& /* args */)
{
    // TODO: Add code that deals with background task progress.
} };
auto progress = [this](BackgroundTaskRegistration^ task, BackgroundTaskProgressEventArgs^ args)
{
    // TODO: Add code that deals with background task progress.
};

手順 2

バックグラウンド タスクの完了を処理するコードをイベント ハンドラーに追加します。

たとえば、バックグラウンド タスクのサンプル は、args パラメーターを介して渡された進行状況で UI を更新します。

private void OnProgress(IBackgroundTaskRegistration task, BackgroundTaskProgressEventArgs args)
{
    var progress = "Progress: " + args.Progress + "%";
    BackgroundTaskSample.SampleBackgroundTaskProgress = progress;
    UpdateUI();
}
auto progress{ [this](
    Windows::ApplicationModel::Background::BackgroundTaskRegistration const& /* sender */,
    Windows::ApplicationModel::Background::BackgroundTaskProgressEventArgs const& args)
{
    winrt::hstring progress{ L"Progress: " + winrt::to_hstring(args.Progress()) + L"%" };
    BackgroundTaskSample::SampleBackgroundTaskProgress = progress;
    UpdateUI();
} };
auto progress = [this](BackgroundTaskRegistration^ task, BackgroundTaskProgressEventArgs^ args)
{
    auto progress = "Progress: " + args->Progress + "%";
    BackgroundTaskSample::SampleBackgroundTaskProgress = progress;
    UpdateUI();
};

イベント ハンドラー関数を新規および既存のバックグラウンド タスクに登録する

手順 1

アプリが初めてバックグラウンド タスクを登録するときは、アプリがまだフォアグラウンドでアクティブな状態でタスクが実行されている場合に備えて、進行状況と完了の更新を受け取るために登録する必要があります。

たとえば、バックグラウンド タスクのサンプル は、登録する各バックグラウンド タスクで次の関数を呼び出します。

private void AttachProgressAndCompletedHandlers(IBackgroundTaskRegistration task)
{
    task.Progress += new BackgroundTaskProgressEventHandler(OnProgress);
    task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
}
void SampleBackgroundTask::AttachProgressAndCompletedHandlers(Windows::ApplicationModel::Background::IBackgroundTaskRegistration const& task)
{
    auto progress{ [this](
        Windows::ApplicationModel::Background::BackgroundTaskRegistration const& /* sender */,
        Windows::ApplicationModel::Background::BackgroundTaskProgressEventArgs const& args)
    {
        winrt::hstring progress{ L"Progress: " + winrt::to_hstring(args.Progress()) + L"%" };
        BackgroundTaskSample::SampleBackgroundTaskProgress = progress;
        UpdateUI();
    } };

    task.Progress(progress);

    auto completed{ [this](
        Windows::ApplicationModel::Background::BackgroundTaskRegistration const& /* sender */,
        Windows::ApplicationModel::Background::BackgroundTaskCompletedEventArgs const& /* args */)
    {
        UpdateUI();
    } };

    task.Completed(completed);
}
void SampleBackgroundTask::AttachProgressAndCompletedHandlers(IBackgroundTaskRegistration^ task)
{
    auto progress = [this](BackgroundTaskRegistration^ task, BackgroundTaskProgressEventArgs^ args)
    {
        auto progress = "Progress: " + args->Progress + "%";
        BackgroundTaskSample::SampleBackgroundTaskProgress = progress;
        UpdateUI();
    };

    task->Progress += ref new BackgroundTaskProgressEventHandler(progress);

    auto completed = [this](BackgroundTaskRegistration^ task, BackgroundTaskCompletedEventArgs^ args)
    {
        UpdateUI();
    };

    task->Completed += ref new BackgroundTaskCompletedEventHandler(completed);
}

手順 2

アプリが起動するか、バックグラウンド タスクの状態が関連する新しいページに移動すると、現在登録されているバックグラウンド タスクの一覧を取得し、進行状況と完了イベント ハンドラー関数に関連付ける必要があります。 アプリケーションによって現在登録されているバックグラウンド タスクの一覧は、BackgroundTaskRegistrationAllTasks プロパティに保持されます。

たとえば、バックグラウンド タスクのサンプル では、SampleBackgroundTask ページが移動したときに、次のコードを使用してイベント ハンドラーをアタッチします。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    foreach (var task in BackgroundTaskRegistration.AllTasks)
    {
        if (task.Value.Name == BackgroundTaskSample.SampleBackgroundTaskName)
        {
            AttachProgressAndCompletedHandlers(task.Value);
            BackgroundTaskSample.UpdateBackgroundTaskStatus(BackgroundTaskSample.SampleBackgroundTaskName, true);
        }
    }

    UpdateUI();
}
void SampleBackgroundTask::OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs const& /* e */)
{
    // A pointer back to the main page. This is needed if you want to call methods in MainPage such
    // as NotifyUser().
    m_rootPage = MainPage::Current;

    // Attach progress and completed handlers to any existing tasks.
    auto allTasks{ Windows::ApplicationModel::Background::BackgroundTaskRegistration::AllTasks() };

    for (auto const& task : allTasks)
    {
        if (task.Value().Name() == SampleBackgroundTaskName)
        {
            AttachProgressAndCompletedHandlers(task.Value());
            break;
        }
    }

    UpdateUI();
}
void SampleBackgroundTask::OnNavigatedTo(NavigationEventArgs^ e)
{
    // A pointer back to the main page.  This is needed if you want to call methods in MainPage such
    // as NotifyUser().
    rootPage = MainPage::Current;

    // Attach progress and completed handlers to any existing tasks.
    auto iter = BackgroundTaskRegistration::AllTasks->First();
    auto hascur = iter->HasCurrent;
    while (hascur)
    {
        auto cur = iter->Current->Value;

        if (cur->Name == SampleBackgroundTaskName)
        {
            AttachProgressAndCompletedHandlers(cur);
            break;
        }

        hascur = iter->MoveNext();
    }

    UpdateUI();
}