Asynchronous thread-based task processing method and system

By dividing tasks with long execution time into pre-, execution and post-stage stages, and opening up asynchronous thread processing execution stages in the main thread, the problem of UI thread blocking in the existing technology is solved, and task processing efficiency and system stability are improved.

CN119938251APending Publication Date: 2025-05-06PU HUA KE JI YOU XIAN GONG SI
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411802902.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-09
Publication Date
2025-05-06

AI Technical Summary

Technical Problem

When the prior art handles tasks with long execution time, it is easy to cause UI thread blockage, resulting in interface lag or unresponsiveness, and the granularity control is relatively coarse, making it impossible to optimize according to historical trends, which increases development and maintenance costs.

Method used

The tasks with long execution time are divided into pre-stage, execution stage and post-stage, and asynchronous threads are opened in the main thread, the main thread executes pre-stage and post-stage, and the asynchronous thread handles the execution stage, and an exception catching mechanism is used to handle exceptions in the execution stage.

Benefits of technology

By processing long-term tasks through asynchronous threads, the main thread resource occupation is avoided, task processing efficiency is improved, the UI interface is lagged, and the system stability and responsiveness are enhanced.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119938251A_ABST
    Figure CN119938251A_ABST
Patent Text Reader

Abstract

The invention provides a task processing method and system based on an asynchronous thread, and the method comprises the steps: dividing a task with long execution time into a pre-stage, an execution stage and a post-stage, the pre-stage is responsible for processing a user event triggering operation, the execution stage is responsible for processing an operation with relatively long time consumption, and the post-stage is responsible for processing an operation with relatively long time consumption; the post-stage is responsible for processing the operation after the operation with relatively long time consumption; and opening up an asynchronous thread in the main thread, executing the pre-stage in the main thread, processing the execution stage in the asynchronous thread, and notifying the main thread to execute the post-stage by the asynchronous thread after the execution stage is processed. According to the method, the tasks with long execution time are divided into different processing stages, and the stages with relatively long time consumption are processed by the asynchronous thread, so that resource occupation of the main thread is avoided, and the overall task processing efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of task execution, and more specifically, to a task processing method and system based on asynchronous threads. Background Art

[0002] In MFC, when you click a button or perform other operations, the corresponding hook function (event processing function) will be called. If the task takes too long to execute, such as time-consuming operations such as complex calculations or file downloads, the message loop will be unable to continue processing other messages, resulting in the UI becoming unresponsive and a "white screen" appearing in the Windows window, affecting the smoothness of the MFC window.

[0003] The common way to deal with this is to split long tasks into small, interruptible tasks to avoid blocking the UI thread. First, start the timer, execute the tasks in segments in the timer callback function, and check whether the tasks are completed. If they are completed, stop the timer, otherwise continue to execute the segmented tasks. While executing the tasks, update the UI response of the window.

[0004] This approach has the following disadvantages: 1. Coarse granularity control: Tasks need to be split into small segments, and the execution time of each segment should be as short as possible, otherwise it may still cause interface freezes. The auxiliary decisions given are relatively rigid and based on data, and cannot be analyzed based on historical trends.

[0005] 2. Not suitable for very time-consuming operations: If each fragment execution takes a long time, the user experience will be poor, and the timer cannot solve the problem well.

[0006] 3. Long tasks need to be split into multiple small segments, which increases development and maintenance costs. Summary of the invention

[0007] The present invention aims at the technical problems existing in the prior art and provides a task processing method and system based on asynchronous threads. According to a first aspect of the present invention, there is provided a task processing method based on an asynchronous thread, comprising: Divide the tasks with long execution time into a pre-stage, an execution stage and a post-stage, wherein the pre-stage is responsible for processing the user event triggering operation, the execution stage is responsible for processing the operation that takes a long time, and the post-stage is responsible for processing the operation after the operation that takes a long time; An asynchronous thread is opened in the main thread, the pre-stage is executed in the main thread, the execution stage is processed in the asynchronous thread, and after the execution stage is processed, the asynchronous thread notifies the main thread to execute the post-stage.

[0008] Based on the above technical solution, the present invention can also make the following improvements.

[0009] Optionally, executing the pre-stage in the main thread includes: The hook function within the MFC framework based on the main thread captures the user event triggering operations on the UI interface.

[0010] Optionally, in the process of processing the execution phase in the asynchronous thread, the process further includes: using an exception capture mechanism to capture whether an exception occurs in the execution phase; If an exception occurs during the execution phase, the asynchronous thread is terminated, and a notification is sent to the main thread through a custom message, so that the main thread reminds the user of the execution failure through the UI interface.

[0011] Optionally, the adopting an exception capture mechanism to capture whether an exception occurs in the execution phase includes: The log information generated in the execution phase is obtained, the log information is matched with the exception information in the exception knowledge base, and the exception in the execution phase is captured.

[0012] Optionally, after the execution phase is completed, the asynchronous thread notifies the main thread to execute the post-phase, including: If no exception is caught during the execution phase, and the asynchronous thread completes the execution phase, the processed data is encapsulated as a custom message and placed in the main thread. In the asynchronous thread, the main thread is notified to execute the post-phase through the PostMessage function.

[0013] Optionally, open multiple asynchronous threads in the main thread; The operation that takes a long time in the execution phase is divided into multiple sub-operations, and the multiple sub-operations are executed based on multiple asynchronous threads. After the execution is completed, each asynchronous thread encapsulates the processed sub-operation data into a custom message, places it in the queue of the main thread, and notifies the main thread to execute the post-phase; The main thread merges multiple sub-operation data in the queue and executes the post-stage.

[0014] Optionally, if there are multiple tasks with long execution time that need to be processed simultaneously, each task with long execution time is divided into a pre-stage, an execution stage, and a post-stage; Open multiple asynchronous threads in the main thread; A pre-phase of multiple long-running tasks is executed in the main thread, each long-running phase is processed in each asynchronous thread, and a post-phase of multiple long-running tasks is executed in the main thread.

[0015] According to a second aspect of the present invention, there is provided a task processing system based on an asynchronous thread, comprising: A division module is used to divide the task with a long execution time into a pre-stage, an execution stage and a post-stage, wherein the pre-stage is responsible for processing the user event triggering operation, the execution stage is responsible for processing the operation that takes a long time, and the post-stage is responsible for processing the operation after the operation that takes a long time; The processing module is used to open an asynchronous thread in the main thread; and process the pre-stage in the main thread, process the execution stage in the asynchronous thread, and process the post-stage in the main thread.

[0016] According to a third aspect of the present invention, there is provided an electronic device comprising a memory and a processor, wherein the processor is configured to implement the steps of an asynchronous thread-based task processing method when executing a computer management program stored in the memory.

[0017] According to a fourth aspect of the present invention, there is provided a computer-readable storage medium on which a computer management program is stored. When the computer management program is executed by a processor, the steps of the task processing method based on asynchronous threads are implemented.

[0018] The present invention provides a task processing method and system based on asynchronous threads, which divides tasks with long execution time into a pre-stage, an execution stage and a post-stage; an asynchronous thread is opened in the main thread, the pre-stage is executed in the main thread, the execution stage is processed in the asynchronous thread, and after the execution stage is processed, the asynchronous thread notifies the main thread to execute the post-stage. The present invention divides tasks with long execution time into different processing stages, and the time-consuming stages are handed over to the asynchronous thread for processing, thereby avoiding resource occupation of the main thread and improving the overall task processing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 A flowchart of a task processing method based on asynchronous threads provided by the present invention; Figure 2 It is a schematic diagram of the overall process of the task processing method based on asynchronous threads; Figure 3 A schematic diagram of the structure of a task processing system based on asynchronous threads provided by the present invention; Figure 4 A schematic diagram of the hardware structure of a possible electronic device provided by the present invention; Figure 5 A schematic diagram of the hardware structure of a possible computer-readable storage medium provided by the present invention. DETAILED DESCRIPTION

[0020] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention. In addition, the technical features in the various embodiments or single embodiments provided by the present invention can be arbitrarily combined with each other to form a feasible technical solution. This combination is not subject to the constraints of the sequence of steps and / or the structural composition mode, but must be based on the ability of ordinary technicians in this field to achieve. When the combination of technical solutions is contradictory or cannot be achieved, it should be considered that such a combination of technical solutions does not exist and is not within the scope of protection required by the present invention.

[0021] Figure 1 A flowchart of a task processing method based on asynchronous threads provided by the present invention is shown in FIG. Figure 1 As shown, the method includes: Step 1, divide the task with long execution time into a pre-stage, an execution stage and a post-stage, wherein the pre-stage is responsible for processing the user event triggering operation, the execution stage is responsible for processing the time-consuming operation, and the post-stage is responsible for processing the operation after the time-consuming operation.

[0022] It is understandable that the user can click on the UI interface with a mouse or a button to trigger an event, such as clicking to transfer files or upload data. For tasks that take a long time, such as a large file to transfer or a large amount of data to upload, the response of the UI will be affected. In the present invention, for tasks that take a long time to execute, operations are performed in stages. Specifically, tasks that take a long time to execute are divided into three stages, namely, the pre-stage, the execution stage, and the post-stage. Among them, the pre-stage is a hook function in the MFC framework, that is, a function triggered by a user event, such as: a button click; the execution stage handles time-consuming operations; and the post-stage is responsible for the content after the time-consuming operations. The pre-stage is in the early stage of operations that interact with the UI, the execution stage handles time-consuming operations, and the post-stage handles operations that interact with the UI in the later stage.

[0023] Step 2, opening an asynchronous thread in the main thread, executing the pre-stage in the main thread, processing the execution stage in the asynchronous thread, and after the execution stage is completed, the asynchronous thread notifies the main thread to execute the post-stage.

[0024] It is understandable that an asynchronous thread is opened in the main thread, wherein the main thread executes the operations of the pre-stage and the post-stage, and the operations of the execution stage are processed by the asynchronous thread.

[0025] Among them, in the process of asynchronous thread processing operations in the execution phase, an exception capture mechanism is used to capture whether an exception occurs in the execution phase; if an exception occurs in the execution phase, the asynchronous thread is terminated, and the main thread is notified through a custom message, so that the main thread reminds the user of the execution failure through the UI interface.

[0026] In the process of the asynchronous thread processing execution phase, log information in the processing process is obtained, the log information is matched with the exception information in the exception knowledge base, and the exception in the execution phase is captured.

[0027] If no exception occurs when the asynchronous thread is processing the operations in the execution phase, after the asynchronous thread completes the processing, it encapsulates the post-phase into a custom message and places it in the main thread. In the asynchronous thread, the main thread is notified to execute the post-phase through the PostMessage function.

[0028] The main thread executes the post-processing phase according to the data processed by the asynchronous thread, for example, displaying the data processed by the asynchronous thread on the UI interface.

[0029] If the operation in the execution phase takes a long time, the operation that takes a long time in the execution phase can be divided into multiple sub-operations, and multiple asynchronous threads are opened in the main thread. Multiple sub-operations are executed based on multiple asynchronous threads. After the execution is completed, each asynchronous thread encapsulates the processed sub-operation data into a custom message, puts it in the queue of the main thread, and notifies the main thread to execute the post-stage.

[0030] The main thread merges multiple sub-operation data in the queue in sequence and executes the post-stage.

[0031] In addition, if there are multiple tasks with long execution time that need to be processed simultaneously, each task with long execution time is divided into a pre-stage, an execution stage, and a post-stage. Multiple asynchronous threads are opened in the main thread, and the pre-stages of multiple tasks with long execution time are executed in the main thread. Each execution stage of a long execution time is processed in each asynchronous thread. Multiple asynchronous threads can process the execution stages of multiple tasks at the same time, and the post-stages of multiple tasks with long execution time are executed in the main thread.

[0032] See also Figure 2, which is an overall flow chart of a task processing method based on asynchronous threads provided by the present invention. First, the user clicks a button on the UI interface to divide the corresponding task into a pre-stage, an execution stage, and a post-stage. An asynchronous thread is opened in the main thread to process the execution stage of the task. When the asynchronous thread processes the execution stage, it captures exceptions in the processing process. If an exception is found, the asynchronous thread is terminated and a notification is sent to the main thread. If no exception occurs, the custom message is encapsulated to pass through the main thread without exception, and the main thread is notified to execute the post-stage, and the main thread performs the post-operation.

[0033] See also Figure 3 , provides a task processing system based on asynchronous threads of the present invention, the system comprising: a division module 301, used to divide a task with a long execution time into a pre-stage, an execution stage and a post-stage, the pre-stage is responsible for processing user event triggering operations, the execution stage is responsible for processing time-consuming operations, and the post-stage is responsible for processing operations after the time-consuming operations; The processing module 302 is used to open an asynchronous thread in the main thread; and process the pre-stage in the main thread, process the execution stage in the asynchronous thread, and process the post-stage in the main thread.

[0034] It can be understood that the task processing system based on asynchronous threads provided by the present invention corresponds to the task processing methods based on asynchronous threads provided by the aforementioned embodiments. The relevant technical features of the task processing system based on asynchronous threads can refer to the relevant technical features of the task processing method based on asynchronous threads, which will not be repeated here.

[0035] See also Figure 4 , Figure 4 Schematic diagram of an electronic device provided by an embodiment of the present invention. Figure 4 As shown, an embodiment of the present invention provides an electronic device 400, including a memory 410, a processor 420 and a computer program 411 stored in the memory 410 and executable on the processor 420, wherein the processor 420 implements the following steps when executing the computer program 411: dividing a task with a long execution time into a pre-stage, an execution stage and a post-stage, wherein the pre-stage is responsible for processing user event triggering operations, the execution stage is responsible for processing time-consuming operations, and the post-stage is responsible for processing operations subsequent to the time-consuming operations; opening an asynchronous thread in the main thread, executing the pre-stage in the main thread, processing the execution stage in the asynchronous thread, and after the execution stage is processed, the asynchronous thread notifies the main thread to execute the post-stage.

[0036] See also Figure 5 , Figure 4Schematic diagram of an embodiment of a computer-readable storage medium provided by the present invention. Figure 5 As shown, this embodiment provides a computer-readable storage medium 500, on which a computer program 511 is stored. When the computer program 511 is executed by a processor, the following steps are implemented: tasks with long execution time are divided into a pre-stage, an execution stage and a post-stage, wherein the pre-stage is responsible for processing user event triggering operations, the execution stage is responsible for processing time-consuming operations, and the post-stage is responsible for processing operations after the time-consuming operations; an asynchronous thread is opened in the main thread, the pre-stage is executed in the main thread, the execution stage is processed in the asynchronous thread, and after the execution stage is processed, the asynchronous thread notifies the main thread to execute the post-stage.

[0037] The present invention provides an asynchronous thread-based task processing method and system, which have the following beneficial effects: (1) By executing time-consuming tasks in asynchronous threads, the main thread can continue to respond to user operations, achieving barrier-free message loops, avoiding UI interface freezes or unresponsiveness, and improving user experience.

[0038] (2) The segmented execution method allows tasks to be completed in multiple stages, with timely feedback or continued execution at each stage, avoiding long-term operation blocking and ensuring that the application always maintains a high level of responsiveness.

[0039] (3) Through the exception capture mechanism, errors in the execution phase can be captured and processed in a timely manner, preventing exceptions from causing thread crashes or data inconsistency, thereby enhancing the robustness and stability of the system.

[0040] (4) The task is divided into three stages: pre-processing, execution, and post-processing. It can be optimized according to the characteristics of the task. The execution stage runs independently in an asynchronous thread, avoiding resource occupation of the main thread and improving the overall task processing efficiency. Through the custom message and PostMessage mechanism, it ensures that the UI update and post-processing operations can be executed smoothly in the main thread, avoiding problems that may arise when updating the UI across threads.

[0041] It should be noted that in the above embodiments, the description of each embodiment has its own emphasis, and for parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0042] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0043] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded computer, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0044] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0045] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0046] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.

[0047] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.

Claims

1. A task processing method based on asynchronous threads, characterized in that: include: Divide the tasks with long execution time into a pre-stage, an execution stage and a post-stage, wherein the pre-stage is responsible for processing the user event triggering operation, the execution stage is responsible for processing the operation that takes a long time, and the post-stage is responsible for processing the operation after the operation that takes a long time; An asynchronous thread is opened in the main thread, the pre-stage is executed in the main thread, the execution stage is processed in the asynchronous thread, and after the execution stage is processed, the asynchronous thread notifies the main thread to execute the post-stage.

2. The task processing method based on asynchronous thread according to claim 1, characterized in that: The performing of the pre-stage in the main thread includes: The hook function within the MFC framework based on the main thread captures the user event triggering operations on the UI interface.

3. The task processing method based on asynchronous thread according to claim 1, characterized in that: The process of processing the execution phase in the asynchronous thread also includes: using an exception capture mechanism to capture whether an exception occurs in the execution phase; If an exception occurs during the execution phase, the asynchronous thread is terminated, and a notification is sent to the main thread through a custom message, so that the main thread reminds the user of the execution failure through the UI interface.

4. The asynchronous thread-based task processing method according to claim 3, characterized in that: The adopting of an exception capture mechanism to capture whether an exception occurs in the execution phase includes: The log information generated in the execution phase is obtained, the log information is matched with the exception information in the exception knowledge base, and the exception in the execution phase is captured.

5. The task processing method based on asynchronous thread according to claim 1, characterized in that: After the execution phase is completed, the asynchronous thread notifies the main thread to execute the post-phase, including: If no exception is caught during the execution phase, and the asynchronous thread completes the execution phase, the processed data is encapsulated as a custom message and placed in the main thread. In the asynchronous thread, the main thread is notified to execute the post-phase through the PostMessage function.

6. The task processing method based on asynchronous thread according to claim 1, characterized in that: Open multiple asynchronous threads in the main thread; The operation that takes a long time in the execution phase is divided into multiple sub-operations, and the multiple sub-operations are executed based on multiple asynchronous threads. After the execution is completed, each asynchronous thread encapsulates the processed sub-operation data into a custom message, places it in the queue of the main thread, and notifies the main thread to execute the post-phase; The main thread merges multiple sub-operation data in the queue and executes the post-stage.

7. The task processing method based on asynchronous thread according to claim 1, characterized in that: If there are multiple tasks with long execution time that need to be processed simultaneously, divide each task with long execution time into the pre-stage, execution stage and post-stage; Open multiple asynchronous threads in the main thread; A pre-phase of multiple long-running tasks is executed in the main thread, each long-running phase is processed in each asynchronous thread, and a post-phase of multiple long-running tasks is executed in the main thread.

8. A task processing system based on asynchronous threads, characterized in that: include: A division module is used to divide the task with a long execution time into a pre-stage, an execution stage and a post-stage, wherein the pre-stage is responsible for processing the user event triggering operation, the execution stage is responsible for processing the operation that takes a long time, and the post-stage is responsible for processing the operation after the operation that takes a long time; Processing module, used to open asynchronous threads in the main thread; And the pre-stage is processed in the main thread, the execution stage is processed in the asynchronous thread, and the post-stage is processed in the main thread.

9. An electronic device, characterized in that: The invention comprises a memory and a processor, wherein the processor is used to implement the steps of a task processing method based on an asynchronous thread when executing a computer management program stored in the memory.

10. A computer-readable storage medium, characterized in that: A computer management program is stored thereon, and when the computer management program is executed by the processor, the steps of the task processing method based on asynchronous threads are implemented.

Citation Information

Cited By

  • Detection and transmission performance optimization method, device and system for dynamic photo

    CN121239675A