An event processing method and apparatus

By introducing a proxy thread into the application process, the problem of application unresponsiveness can be detected and handled by the main thread, thus improving the user experience.

CN119883667BActive Publication Date: 2025-12-19BEIJING ZITIAO NETWORK TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202311376519.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-10-23
Publication Date
2025-12-19
Estimated Expiration
2043-10-23

AI Technical Summary

Technical Problem

The application becomes unresponsive when handling input events, impacting the user experience.

Method used

By introducing a proxy thread into the application process, the main thread is detected to be lagging. When a lag is detected, the system process is sent an input event completion indication message in advance to avoid the response waiting time recorded by the system process exceeding the timeout threshold.

Benefits of technology

This effectively avoids application unresponsiveness (ANR) and improves the user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119883667B_ABST
    Figure CN119883667B_ABST
Patent Text Reader

Abstract

The application discloses an event processing method, which can be applied to an application process. The application process can include a main thread and an agent thread. The agent thread can detect whether the main thread is stuck, and when detecting that the main thread is stuck, acquire an input event sent by a system process to the main thread, and send completion indication information for the input event to the system process. That is, in the case that the main thread cannot send the completion indication information of the input event to the system process in time, the agent thread sends the completion indication information of the input event to the system process in advance, so that the system process can receive the completion indication information of the input event in time, thereby avoiding that the system process controls the application process to generate ANR. Therefore, by using the scheme provided in the embodiments of the application, ANR can be effectively reduced.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the field of event processing, and in particular to an event processing method and device. BACKGROUND

[0002] With the development of computer technology, more and more application programs appear. At present, in the process of using an application program, the user may encounter the phenomenon that the application program does not respond. As an example, when the user uses an application program installed on a terminal device, the user can trigger an operation through the terminal device, and correspondingly, the terminal device can generate an input event in response to the user's triggering operation. If the application program does not process the input event within a certain time period, the application program does not respond (ANR) will occur.

[0003] The occurrence of the application program not responding will affect the user's experience of using the application program, and therefore, there is an urgent need for a solution to solve this problem. SUMMARY

[0004] In order to solve or at least partially solve the above technical problems, the embodiments of the present application provide an event processing method and device.

[0005] In a first aspect, the embodiments of the present application provide an event processing method applied to an application process, the application process including a main thread and a proxy thread, and the method includes:

[0006] The proxy thread detects whether the main thread is stuck;

[0007] The proxy thread acquires an input event sent by a system process to the main thread in response to detecting that the main thread is stuck, and sends completion indication information for the input event to the system process, the completion indication information indicating that the input event has been processed.

[0008] Optionally, the proxy thread acquires an input event sent by a system process to the main thread in response to detecting that the main thread is stuck, and sends completion indication information for the input event to the system process, and the method includes:

[0009] The proxy thread acquires an input event sent by a system process to the main thread in response to detecting that the main thread is stuck and the duration of the main thread being stuck being less than or equal to a preset duration, and sends completion indication information for the input event to the system process.

[0010] Optionally, the proxy thread detects whether the main thread is stuck, and the method includes:

[0011] The proxy thread sends a target task to the main thread;

[0012] The agent thread periodically polls a completion status of the target task to determine whether the main thread is stuck.

[0013] Optionally, the periodically polling the completion status of the target task to determine whether the main thread is stuck comprises:

[0014] For the ith polling, if the completion status of the target thread indicates that the target task has not been completed, it is determined that the main thread is stuck.

[0015] Optionally, the method further comprises:

[0016] A time difference between a time corresponding to the ith polling and a time when the target task is sent to the main thread is determined as a stuck duration of the main thread.

[0017] Optionally, the periodically polling the completion status of the target task to determine whether the main thread is stuck comprises:

[0018] For the ith polling, if the completion status of the target thread indicates that the target task has been completed, it is determined that the main thread is not stuck.

[0019] Optionally, a polling period of the completion status of the target task is less than a timeout response threshold of the system process producing an application unresponsiveness, and the preset time length is greater than the timeout response threshold.

[0020] In a second aspect, an embodiment of the present application provides an event processing apparatus applied to an agent thread of an application process, and the apparatus comprises:

[0021] A detection unit configured to detect whether the main thread is stuck;

[0022] An acquisition unit configured to, in response to detecting that the main thread is stuck, acquire an input event sent by a system process to the main thread;

[0023] A first sending unit configured to send completion indication information for the input event to the system process, the completion indication information indicating that the input event has been processed and completed.

[0024] Optionally, the acquisition unit is configured to:

[0025] In response to detecting that the main thread is stuck and a stuck duration of the main thread being less than or equal to a preset time length, acquire an input event sent by a system process to the main thread;

[0026] The first sending unit is configured to send completion indication information for the input event to the system process.

[0027] Optionally, the detection unit is configured to:

[0028] sending the target task to the main thread;

[0029] periodically polling a completion status of the target task to determine whether the main thread is stuck.

[0030] Optionally, the periodically polling a completion status of the target task to determine whether the main thread is stuck comprises:

[0031] for the ith polling, if the completion status of the target thread indicates that the target task has not been completed, determining that the main thread is stuck.

[0032] Optionally, the apparatus further comprises:

[0033] a second sending unit configured to determine a time difference between a time corresponding to the ith polling and a time of sending the target task to the main thread as a stuck duration of the main thread.

[0034] Optionally, the periodically polling a completion status of the target task to determine whether the main thread is stuck comprises:

[0035] for the ith polling, if the completion status of the target thread indicates that the target task has been completed, determining that the main thread is not stuck.

[0036] Optionally, a polling period of the completion status of the target task is less than a timeout response threshold of the system process producing an application unresponsiveness, and the preset duration is greater than the timeout response threshold.

[0037] In a third aspect, an embodiment of the present application provides an electronic device, the electronic device comprising a processor and a memory;

[0038] The processor is configured to execute instructions stored in the memory, so that the electronic device performs the method in any one of the above first aspect.

[0039] In a fourth aspect, an embodiment of the present application provides a computer readable storage medium comprising instructions, the instructions instructing a device to perform the method in any one of the above first aspect.

[0040] In a fifth aspect, an embodiment of the present application provides a computer program product, when the computer program product runs on a computer, causing the computer to perform the method in any one of the above first aspect.

[0041] Compared with the prior art, the embodiments of the present application have the following advantages:

[0042] The embodiment of the present application provides an event processing method, which can be applied to an application process. The application process can include a main thread and an agent thread. In the embodiment of the present application, when the main thread is stuck, the input event sent by a system process to the main thread can not be processed in time, and accordingly, when the response waiting time recorded by the system process for the input event exceeds a timeout response threshold, the completion indication information of the input event can not be received, thereby causing the system process to control the application process to generate ANR. The completion indication information is used to indicate that the input event has been processed. Therefore, in the embodiment of the present application, the agent thread can detect whether the main thread is stuck, and when it is detected that the main thread is stuck, the input event sent by the system process to the main thread is acquired, and the completion indication information of the input event is sent to the system process. That is, in the case that the main thread cannot send the completion indication information of the input event to the system process in time, the agent thread sends the completion indication information of the input event to the system process in advance, so that the system process can receive the completion indication information of the input event in time, thereby avoiding that the system process controls the application process to generate ANR. Therefore, by using the present solution, ANR can be effectively reduced. BRIEF DESCRIPTION OF DRAWINGS

[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the drawings needed to be used in the embodiments or the prior art description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments described in the present application, and for those skilled in the art, other drawings can also be obtained without creative labor on the basis of these drawings.

[0044] Figure 1 An input event processing flowchart provided by the embodiment of the present application;

[0045] Figure 2 A flowchart of an event processing method provided by the embodiment of the present application;

[0046] Figure 3 An input event processing flowchart provided by the embodiment of the present application;

[0047] Figure 4 A flowchart of another event processing method provided by the embodiment of the present application;

[0048] Figure 5 A structural diagram of an event processing device provided by the embodiment of the present application. DETAILED DESCRIPTION

[0049] In the following, the technical solutions in the embodiments of the present application will be described clearly and completely in conjunction with the accompanying drawings of the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all the embodiments of the present application. Based on the embodiments in the present application, all the other embodiments obtained by those skilled in the art without creative effort belong to the scope of the present application.

[0050] The inventors of the present application have found that, in the process of using an application program, the application program may not respond. As an example, when a user uses an application program installed on a terminal device, the user can trigger an operation through the terminal device, and accordingly, the terminal device can generate an input event in response to the triggering operation of the user. If the application program does not process the input event within a certain time period, ANR occurs. It can be understood that Figure 1 Figure 1 A flowchart of an input event processing process provided by an embodiment of the present application is shown in FIG. 1.

[0051] As shown in FIG. 1, a system service (system_server) process can generate an input event in response to a triggering operation of a user, and send the input event to a main thread of an application process through an input event dispatch (InputDispatcher) thread. The main thread of the application process processes the input event, and after the processing is completed, sends completion indication information for the input event to the input event dispatch thread. After the system service process sends the input event to the main thread, the system service process starts timing to record the response waiting time for the input event. If the timing reaches a timeout response threshold, and the input event dispatch thread has not received the completion indication information for the input event, the system service process can control the application process to generate ANR. The system service process controlling the application process to generate ANR can be controlling the application process to display prompt information that the application program cannot respond in the form of a pop-up window, or killing the application process, which is not limited in the embodiments of the present application. Figure 1

[0052] The occurrence of application program non-response will affect the user experience of using the application program. For example, displaying prompt information that the application program cannot respond in the form of a pop-up window will disturb the user, and killing the application process will make the user unable to continue using the application program.

[0053] To solve the above problems, the embodiments of the present application provide an event processing method and device.

[0054] In the following, various non-limiting embodiments of the present application will be described in detail in conjunction with the accompanying drawings.

[0055] ​​Exemplary method

[0056] Referring to Figure 2 FIG. 1 is a flow diagram of an event processing method according to an embodiment of the present application. Figure 2 The method shown can be applied to an application process, which can include a main thread and an agent thread. In this embodiment, the method can include the following steps, for example: S101-S102.

[0057] S101: The agent thread detects whether the main thread is stuck.

[0058] In this embodiment, considering that when the main thread is stuck, the input event sent by the system process to the main thread can not be processed in time, and accordingly, when the response waiting time recorded by the system process for the input event exceeds the timeout response threshold, the completion indication information of the input event can not be received, thereby causing the system process to control the application process to generate ANR. Therefore, the agent thread can detect whether the main thread is stuck, and accordingly, determine further execution measures according to the detection result. For the response waiting time, please refer to the related content in the foregoing description of Figure 1 , which will not be described here.

[0059] The agent thread can detect whether the main thread is stuck in various ways. Next, two possible implementation ways are introduced.

[0060] In one example, the agent thread can query the message cache queue of the main thread, and determine whether the main thread is stuck according to the state of the message cache queue. For example, if the usage rate of the message cache queue is higher than a certain threshold, it is determined that the main thread is stuck, and if the usage rate of the message cache queue is lower than or equal to the foregoing threshold, it is determined that the main thread is not stuck. The message cache queue of the main thread is used to cache messages to be processed, and the foregoing input event can be one of the message types to be processed by the main thread.

[0061] In another example, the agent thread can send target tasks to the main thread and periodically poll the completion status of the target tasks to determine whether the main thread is stuck. Specifically, the agent thread can send the target tasks to the main thread according to a certain period (for example, 2 seconds), and for each target task, the agent thread can periodically poll the completion status of the target task to determine whether the main thread is stuck. In one specific example, the completion status of the target task can be periodically polled within a preset time period to determine whether the main thread is stuck.

[0062] The target task may be, for example, a state setting task of a main thread, and the initial state of the main thread may be, for example, false. If the target task is processed by the main thread, the state of the main thread is set to true.

[0063] The period of polling the completion state of the target task is not specifically limited in the embodiments of the present application, and may be determined according to actual conditions. In an example, the period may be any value less than the timeout response threshold of the system process to generate an application unresponsive (ANR), so that the main thread is determined to be stuck in time in advance in the case where the ANR is not generated in a traditional process. For example, if the timeout response threshold is 5 s, the period of polling the completion state of the target task may be 2 s.

[0064] In an example, if the main thread is stuck, the main thread will not process the target task in time. Therefore, for the i th polling of the completion state of the target task, if the completion state of the target thread indicates that the target task has not been completed, the agent thread may determine that the main thread is stuck. Correspondingly, if the completion state of the target thread indicates that the target task has been completed, it may be determined that the main thread is not stuck.

[0065] S102: In response to detecting that the main thread is stuck, the agent thread acquires an input event sent by a system process to the main thread, and sends completion indication information for the input event to the system process, where the completion indication information indicates that the input event has been processed.

[0066] In the embodiments of the present application, since the main thread being stuck may cause the ANR, once the agent thread detects that the main thread is stuck, it may acquire an input event sent by a system process to the main thread in response to detecting that the main thread is stuck, and further send completion indication information for the input event to the system process. After the system process receives the completion indication information for the input event, the completion state of the input event is changed, and the application process is not controlled to generate the ANR.

[0067] That is, in the embodiments of the present application, the agent thread may acquire an input event sent by a system process to the main thread in advance when the main thread is stuck, and send completion indication information for the input event to the system process in advance, so that the system process can receive the completion indication information for the input event in time, thereby avoiding the system process controlling the application process to generate the ANR.

[0068] In one example, the agent thread can acquire a socket file descriptor (socket fd) used by the main thread and the system process to communicate, and acquire the input event sent by the system process to the main thread through the socket fd. In one example, the agent thread can acquire the socket fd used by the main thread and the system process to communicate by hooking a function used by the application process to receive data.

[0069] In one example, after the agent thread acquires the input event, the agent thread can extract an identifier of the input event, and correspondingly, when sending the completion indication information of the input event to the system process, the agent thread can send the identifier of the input event and the completion indication information to the system process. In one example, the agent thread can send the identifier of the input event and the completion indication information to the system process through the socket fd.

[0070] Here, the system process refers to a process of an operating system. The system process can correspond to a process of a system service. Figure 1 The system service process can send the input event to the main thread of the application process through the input event dispatch thread of the system process. Correspondingly, the agent thread can send the completion indication information of the input event to the input event dispatch thread of the system process.

[0071] In one example, if the main thread is stuck for too long, it indicates that the event processing capability of the main thread is low, and correspondingly, the possibility of normal operation of the application program is also low. In this case, it is reasonable to generate ANR, and in this case, the traditional ANR generation mechanism should not be blocked. If the main thread is not stuck for too long, it indicates that the event processing capability of the main thread can be restored, and in this case, if ANR is generated, it is not conducive to the user to continue using the application program. Therefore, in one implementation, when the agent thread detects that the main thread is stuck and the duration of the main thread being stuck is less than or equal to a preset duration, the agent thread can acquire the input event sent by the system process to the main thread, and send the completion indication information of the input event to the system process. In this way, in the case where the main thread is not stuck for too long, ANR can be avoided, thereby facilitating the user to continue using the application program.

[0072] In one example, if the agent thread determines whether the main thread is stuck by periodically polling the completion status of the target task, if the i-th polling determines that the main thread is stuck, the agent thread can also determine the time difference between the time corresponding to the i-th polling and the time of sending the target task to the main thread as the stuck duration of the main thread. Further, the agent thread can determine whether the stuck duration is less than a preset duration.

[0073] The preset duration is not specifically limited in the embodiments of the present application, and can be determined according to actual conditions. In one example, the preset duration can be any value greater than the timeout response threshold of the system process generating an application unresponsive, so as to allow the main thread to be stuck for a preset duration without generating ANR. For example, if the timeout response threshold is 5s, the preset duration can be 8s.

[0074] The event processing method provided by the embodiments of the present application is introduced above, and next, the event processing method is introduced in combination with a specific example.

[0075] Referring to Figure 3 , the figure is a flowchart of processing an input event provided by an embodiment of the present application. As Figure 3 indicated, the application process includes a main thread and an agent thread, and the interaction flow between the main thread and the system process is the same as Figure 1 , the agent thread is used to send the completion indication information of the input event to the system process in advance when detecting that the main thread is stuck.

[0076] In one specific example, the steps performed by the agent thread can be as Figure 4 indicated, Figure 4 the flowchart of another event processing method provided by an embodiment of the present application.

[0077] As Figure 4 indicated, the agent thread can perform the following S201-S205.

[0078] S201: sending a target task to the main thread, and recording the sending time of sending the target task to the main thread.

[0079] S202: delaying the polling period of the completion status of the target task.

[0080] S202 can be, for example, delaying for 2 seconds in specific implementation.

[0081] S203: querying whether the completion status of the target task is completed.

[0082] If the completion state of the target task is completed, S201 is continued to be executed; if the completion state of the target task is not completed, S204 is continued to be executed.

[0083] S204: determining whether a difference between a current time and the aforementioned sending time is greater than a preset time length.

[0084] If the difference is greater than the preset time length, S202 is executed; if the difference is less than or equal to the preset time length, S205 is executed.

[0085] The difference mentioned herein can correspond to the aforementioned lag time length.

[0086] S205: obtaining an input event sent by a system process to the main thread, and sending completion indication information for the input event to the system process.

[0087] After S205 is executed, S202 is continued to be executed.

[0088] Exemplary device

[0089] Based on the method provided in the foregoing embodiments, the embodiments of the present application further provide an apparatus, which will be described below with reference to the accompanying drawings.

[0090] Referring to Figure 5 , the figure is a structural schematic diagram of an event processing apparatus provided by the embodiments of the present application. Figure 5 The event processing apparatus 500 shown can be applied to an application process, and the application process includes a main thread and an agent thread. Specifically, the apparatus 500 can be applied to the agent thread, and the apparatus 500 includes a detection unit 501, an obtaining unit 502, and a first sending unit 503.

[0091] The detection unit 501 is configured to detect whether the main thread is lagging;

[0092] The obtaining unit 502 is configured to, in response to detecting that the main thread is lagging, obtain an input event sent by a system process to the main thread;

[0093] The first sending unit 503 is configured to send completion indication information for the input event to the system process, and the completion indication information indicates that the input event has been processed and completed.

[0094] Optionally, the obtaining unit 502 is configured to:

[0095] in response to detecting that the main thread is lagging and a time length of the main thread lagging being less than or equal to a preset time length, obtain an input event sent by a system process to the main thread;

[0096] The first sending unit 503 is configured to send completion indication information of the input event to the system process.

[0097] Optionally, the detection unit 501 is configured to:

[0098] send the target task to the main thread;

[0099] periodically poll a completion state of the target task to determine whether the main thread is stuck.

[0100] Optionally, the periodically polling the completion state of the target task to determine whether the main thread is stuck comprises:

[0101] for the i-th polling, if the completion state of the target thread indicates that the target task has not been completed, it is determined that the main thread is stuck.

[0102] Optionally, the apparatus further comprises:

[0103] a second sending unit configured to determine a time difference between a time point corresponding to the i-th polling and a time point of sending the target task to the main thread as a stuck duration of the main thread.

[0104] Optionally, the periodically polling the completion state of the target task to determine whether the main thread is stuck comprises:

[0105] for the i-th polling, if the completion state of the target thread indicates that the target task has been completed, it is determined that the main thread is not stuck.

[0106] Optionally, a polling period of the completion state of the target task is less than a timeout response threshold of the system process producing an application unresponsive, and the preset duration is greater than the timeout response threshold.

[0107] Since the apparatus 500 is corresponding to the method provided in the above method embodiments, the specific implementation of each unit of the apparatus 500 is the same as the same concept as the above method embodiments, and therefore, the specific implementation of each unit of the apparatus 500 can be referred to the description part of the above method embodiments, which will not be described here.

[0108] The embodiment of the present application further provides an electronic device, which comprises a processor and a memory.

[0109] The processor is configured to execute instructions stored in the memory, so that the electronic device executes the event processing method provided in the above method embodiments.

[0110] The embodiment of the present application provides a computer readable storage medium, comprising instructions, the instructions instruct a device to execute the event processing method provided by the above method embodiment.

[0111] The embodiment of the present application provides a computer program product, when the computer program product runs on a computer, makes the computer execute the event processing method provided by the above method embodiment.

[0112] Other embodiments of the application will be apparent to those skilled in the art from consideration of the specification and practice of the application disclosed herein. It is intended that the specification and examples be considered as exemplary only, with the true scope and spirit of the application being indicated by the following claims.

[0113] It should be understood that the application is not limited to the precise construction that has been described above and shown in the accompanying drawings, and that various modifications and changes can be made by those skilled in the art without departing from the scope of the application. The scope of the application is limited only by the claims that follow.

[0114] The above only represents the preferred embodiments of the present application, and is not used to limit the present application, and any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims

1. An event processing method, characterized by, The method is applied to an application process, the application process including a main thread and an agent thread, and the method includes the following steps: The agent thread detects whether the main thread is stuck; The agent thread acquires an input event sent by a system process to the main thread and sends completion indication information for the input event to the system process in response to detecting that the main thread is stuck and a duration of the main thread being stuck being less than or equal to a preset duration, the completion indication information indicating that the input event has been processed, wherein if the duration of the main thread being stuck is less than or equal to the preset duration, it indicates that the event processing capability of the main thread has a possibility of being restored.

2. The method of claim 1, wherein, The agent thread detects whether the main thread is stuck, including: The agent thread sends a target task to the main thread; The agent thread periodically polls a completion state of the target task to determine whether the main thread is stuck.

3. The method of claim 2, wherein, The agent thread periodically polls a completion state of the target task to determine whether the main thread is stuck, including: For the i th polling, if the completion state of the target thread indicates that the target task has not been completed, it is determined that the main thread is stuck.

4. The method of claim 3, wherein, The method further includes: A time difference between a time corresponding to the i th polling and a time when the target task is sent to the main thread is determined as a stuck duration of the main thread.

5. The method of claim 2, wherein, The agent thread periodically polls a completion state of the target task to determine whether the main thread is stuck, including: For the i th polling, if the completion state of the target thread indicates that the target task has been completed, it is determined that the main thread is not stuck.

6. The method according to any one of claims 2-5, characterized in that, A polling period of the completion state of the target task is less than a timeout response threshold of an application program generated by the system process, and the preset duration is greater than the timeout response threshold.

7. An event processing apparatus, characterized by comprising: The device is applied to an agent thread of an application process, and includes: A detection unit configured to detect whether a main thread is stuck; An acquisition unit configured to acquire an input event sent by a system process to the main thread in response to detecting that the main thread is stuck and a duration of the main thread being stuck being less than or equal to a preset duration; A first sending unit configured to send completion indication information for the input event to the system process, the completion indication information indicating that the input event has been processed, wherein if the duration of the main thread being stuck is less than or equal to the preset duration, it indicates that the event processing capability of the main thread has a possibility of being restored.

8. An electronic device, comprising: The electronic device includes a processor and a memory; The processor is configured to execute instructions stored in the memory to enable the electronic device to perform the method of any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that, The instructions instruct a device to perform the method of any one of claims 1 to 6.

Citation Information

Patent Citations

  • Processing device and method for preventing application program from being unresponsive

    CN106155741A