From c2934be8485376571066a652e94ed16ba2bc8d81 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 2 Apr 2021 00:21:03 +0200 Subject: Cancel all tasks when pressing escape to go to previous page or when closing the window --- include/AsyncTask.hpp | 13 +++++++++++++ include/GoogleCaptcha.hpp | 5 +++-- include/QuickMedia.hpp | 11 ++++++----- 3 files changed, 22 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/AsyncTask.hpp b/include/AsyncTask.hpp index 0d9c453..abdc0f8 100644 --- a/include/AsyncTask.hpp +++ b/include/AsyncTask.hpp @@ -26,6 +26,19 @@ namespace QuickMedia { return *this; } + AsyncTask(AsyncTask &&other) { + cancel(); + thread = std::move(other.thread); + future = std::move(other.future); + } + + AsyncTask& operator=(AsyncTask &&other) { + cancel(); + thread = std::move(other.thread); + future = std::move(other.future); + return *this; + } + ~AsyncTask() { cancel(); } diff --git a/include/GoogleCaptcha.hpp b/include/GoogleCaptcha.hpp index 62e47fa..f8063f3 100644 --- a/include/GoogleCaptcha.hpp +++ b/include/GoogleCaptcha.hpp @@ -1,5 +1,6 @@ #pragma once +#include "AsyncTask.hpp" #include #include #include @@ -15,7 +16,7 @@ namespace QuickMedia { }; using RequestChallengeResponse = std::function)>; - std::future google_captcha_request_challenge(const std::string &api_key, const std::string &referer, RequestChallengeResponse challenge_response_callback); + AsyncTask google_captcha_request_challenge(const std::string &api_key, const std::string &referer, RequestChallengeResponse challenge_response_callback); using PostSolutionResponse = std::function, std::optional)>; - std::future google_captcha_post_solution(const std::string &api_key, const std::string &captcha_id, std::array selected_images, PostSolutionResponse solution_response_callback); + AsyncTask google_captcha_post_solution(const std::string &api_key, const std::string &captcha_id, std::array selected_images, PostSolutionResponse solution_response_callback); } \ No newline at end of file diff --git a/include/QuickMedia.hpp b/include/QuickMedia.hpp index 3e8e7c7..497f202 100644 --- a/include/QuickMedia.hpp +++ b/include/QuickMedia.hpp @@ -6,6 +6,7 @@ #include "Storage.hpp" #include "Tab.hpp" #include "MessageQueue.hpp" +#include "AsyncTask.hpp" #include #include #include @@ -65,8 +66,8 @@ namespace QuickMedia { bool fetching_next_page_running = false; int fetched_page = 0; sf::Text search_result_text; - std::future fetch_future; - std::future next_page_future; + AsyncTask fetch_future; + AsyncTask next_page_future; }; class Program { @@ -152,9 +153,9 @@ namespace QuickMedia { std::string manga_id_base64; Json::Value content_storage_json; std::unordered_set watched_videos; - std::future search_suggestion_future; - std::future autocomplete_future; - std::future image_download_future; + AsyncTask search_suggestion_future; + AsyncTask autocomplete_future; + AsyncTask image_download_future; std::thread image_upscale_thead; MessageQueue images_to_upscale_queue; std::vector image_upscale_status; -- cgit v1.2.3