Method for automatically locking screen after disconnection of Windows remote desktop
By monitoring the remote desktop connection status and using the tscon command to restore the session, the problem of automatically locking the screen after the Windows remote desktop connection is disconnected is solved, achieving faster and more stable session recovery, improving work efficiency and system compatibility.
Patent Information
- Application Number
- CN202510356944.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-25
- Publication Date
- 2025-06-27
AI Technical Summary
The Windows remote desktop connection is automatically locked after it is disconnected, resulting in user work interruptions, file loss and system security, which affects work efficiency and continuity in enterprise-level applications.
By monitoring the remote desktop connection status, obtain the user session ID, and use the tscon command to restore the session to avoid automatic screen lock. The method includes real-time process monitoring, error handling and logging, packaged into executable programs to simplify deployment.
It realizes fast and stable recovery of sessions after the remote desktop connection is disconnected, reducing the time and operational complexity of users when they log in again, improving work efficiency, and seamlessly running on a variety of network environments and hardware devices.
Smart Images

Figure CN120216084A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of automatic screen locking, and specifically relates to a method for solving automatic screen locking after the disconnection of Windows Remote Desktop Connection. Background Art
[0002] The Windows operating system is widely used in enterprises and individual users. As an important function, the Remote Desktop Connection (RDC) using the Remote Desktop Protocol (RDP) allows users to remotely control another computer from one computer, enabling efficient and convenient remote work. However, there are many inconveniences in the actual application process of RDC. In particular, the phenomenon of automatic screen locking after the connection is disconnected has a significant impact on users' work.
[0003] Specifically, when a user remotely connects to a target computer, if the connection is unexpectedly disconnected due to reasons such as unstable network, system anomalies, or incorrect operations, the target computer may automatically enter the screen-locked state. Although this phenomenon helps to protect the security of the computer, it brings many inconveniences in actual work. For example, if a user is remotely processing important files and the connection suddenly disconnects, the files may not be saved or may be lost; another example is that the interruption of the remote connection forces the user to log in again, which not only wastes time but may also weaken the system security due to frequent re-logins.
[0004] For enterprise-level applications, this phenomenon may also cause greater troubles. When employees within an enterprise remotely access a server or a workstation, once the connection is disconnected and the screen is automatically locked, it will lead to the interruption of data processing and affect work efficiency. Especially in some work scenarios that require continuous processing of a large amount of data, this phenomenon seriously affects the continuity and efficiency of work. For example, in remote operation and maintenance, operation and maintenance personnel need to frequently view the server status and logs. If they need to log in again every time the connection is interrupted, it not only consumes time but may also cause misjudgment of the server status by the operation and maintenance personnel, affecting the quality of operation and maintenance.
[0005] Existing technical solutions include:
[0006] 1. Modify the system registry settings
[0007] A relatively common solution is to adjust the screen locking behavior after the disconnection of the Remote Desktop Connection by modifying the registry settings of the Windows system. Users can manually edit the relevant key values in the registry, such as multiple sub-items under "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows NT\Terminal Services", to control the Remote Desktop Connection behavior. For example, setting "fDisableLocking" to 1 can prohibit screen locking; setting "fPromptForPassword" to 0 can automatically unlock the screen after disconnection.
[0008] This method of modifying the system registry settings is simple and easy to implement, but it is not 100% successful, and the success rate also varies on different operating systems. In addition, for non-technical personnel, modifying the registry poses certain risks. A slight mistake may lead to system instability or abnormal operation.
[0009] 2. Using third-party software
[0010] There are also some third-party software on the market that can assist in solving the problem of automatic screen locking after the disconnection of the Remote Desktop Connection. For example, some Remote Desktop management tools provide functions such as "Keep Connection" or "Auto Unlock", which can maintain the session or automatically unlock the screen when the connection is unexpectedly disconnected. These software usually have a graphical interface and relatively simple operations, making them suitable for enterprise users in large-scale deployments.
[0011] Using third-party software often requires additional licensing fees and may introduce new security risks after installation, such as vulnerabilities in the software itself or compatibility issues with other parts of the system.
[0012] 3. Custom scripts
[0013] Some technical users control the behavior after the disconnection of the Remote Desktop Connection by writing custom scripts. For example, PowerShell scripts can be used to automatically run the unlock command or the command to keep the session when detecting the disconnection of the Remote Desktop Connection.
[0014] This method of custom scripts is highly flexible, but writing and maintaining scripts requires a relatively high technical level, and it may be difficult to ensure stability in a complex network environment. Summary of the Invention
[0015] In view of the above problems, the present invention is proposed to provide a method for solving the automatic screen locking after the disconnection of the Windows Remote Desktop Connection that overcomes the above problems or at least partially solves the above problems.
[0016] To achieve the above object, the present invention adopts the following technical solutions:
[0017] A method for solving the problem of automatic screen locking after the disconnection of Windows Remote Desktop Connection, the method comprising the following steps:
[0018] S1. Monitor the Remote Desktop connection status;
[0019] S2. Obtain the user session ID;
[0020] S3. Use the tscon command to restore the session;
[0021] S4. Error handling and logging;
[0022] S5. Package it into an executable program.
[0023] Optionally, step S1 includes:
[0024] Perform real-time process monitoring, and use a Python script combined with the Windows API to implement connection status monitoring;
[0025] Perform a process scan once at regular intervals. If no active session is found in three consecutive scans, trigger the recovery mechanism;
[0026] By parsing the entries related to Remote Desktop in the Windows event log and combining the process monitoring results, comprehensively judge the connection status.
[0027] Optionally, step S2 includes:
[0028] Use getpass.getuser() to obtain the username of the current Windows user;
[0029] Call subprocess.Popen to execute the query session command to obtain the current session information;
[0030] Parse the session ID of the current user from the command output and cache the ID.
[0031] Optionally, step S3 includes:
[0032] According to the obtained user session ID, call the system command line through a Python script, and execute the tscon command to reconnect the session to the console;
[0033] Check the execution result of the tscon command. If it fails, retry;
[0034] Record the execution result of each command for subsequent analysis and optimization.
[0035] Optionally, step S4 includes:
[0036] Add an error classification mechanism to the script to distinguish different types of errors;
[0037] For different types of errors, give corresponding prompt messages and record detailed error logs;
[0038] Use a log grading mechanism to distinguish debug logs, warning logs, and error logs.
[0039] Optionally, the error types include connection disconnection, failure to obtain session ID, and failure to execute the tscon command.
[0040] Optionally, step S5 includes:
[0041] Package the Python script into an executable program and support both x86 and x64 architectures;
[0042] Write an installation program to simplify the user deployment process and support silent installation and custom installation paths;
[0043] Provide installation and usage instructions to ensure that users can get started quickly.
[0044] In summary, due to the adoption of the above technical solutions, the beneficial effects of the present invention are:
[0045] The present invention uses Python to obtain the session ID of the current Windows user and uses the tscon command to connect the session to the console (i.e., the desktop), so that the session can be restored more quickly and stably, reducing the time and operation complexity for users to re-login, improving work efficiency, and fully considering compatibility with the existing Windows operating system to ensure seamless operation on various network environments and hardware devices. Description of the Drawings
[0046] Figure 1 It is a schematic flowchart of a method for solving the automatic screen locking after the disconnection of the Windows remote desktop connection provided by an embodiment of the present application. Detailed Embodiments
[0047] To make the objectives, 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 with reference to the drawings in the present invention. Obviously, the described embodiments are some, but not all, of the embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0048] Please refer to Figure 1, this embodiment provides a method for solving the problem of automatic screen locking after the disconnection of the Windows Remote Desktop Connection. The method is characterized in that it includes the following steps:
[0049] S1. Monitor the Remote Desktop connection status.
[0050] Step S1 includes:
[0051] Perform real-time process monitoring, and use a Python script combined with the Windows API (such as the win32api or ctypes library) to implement connection status monitoring.
[0052] Perform a process scan every 5 seconds regularly to balance real-time performance and system resource consumption. If no active session is found in 3 consecutive scans, trigger the recovery mechanism to avoid false triggering caused by short-term network fluctuations.
[0053] By parsing the entries related to the Remote Desktop in the Windows event log and combining the process monitoring results, comprehensively judge the connection status. The dual verification mechanism improves the judgment accuracy and reduces false triggering caused by residual processes.
[0054] S2. Obtain the user session ID.
[0055] Increase support for multi-user sessions to ensure that the session ID of the current user can be accurately obtained in a multi-user environment. Introduce a session ID caching mechanism to reduce repeated queries and improve efficiency.
[0056] Step S2 includes:
[0057] Use getpass.getuser() to obtain the username of the current Windows user.
[0058] Call subprocess.Popen to execute the query session command to obtain the current session information.
[0059] Parse the session ID of the current user from the command output and cache the ID.
[0060] S3. Use the tscon command to restore the session.
[0061] Increase compatibility support for different versions of the Windows system to ensure normal operation in Windows 10, Windows Server 2016 and above versions. Introduce an error retry mechanism to ensure automatic retry when the command execution fails.
[0062] Step S3 includes:
[0063] According to the obtained user session ID, call the system command line through a Python script, and execute the tscon command to reconnect the session to the console.
[0064] Check the execution result of the tscon command, and retry if it fails.
[0065] Record the execution result of each command for subsequent analysis and optimization.
[0066] S4. Error Handling and Logging.
[0067] Add a detailed error classification and handling mechanism to ensure clear prompts can be given in different error situations. Introduce a log grading mechanism to distinguish different levels of log information for subsequent analysis and optimization.
[0068] Step S4 includes:
[0069] Add an error classification mechanism to the script to distinguish different error types, which include disconnection, failure to obtain the session ID, and failure to execute the tscon command, etc.
[0070] For different error types, give corresponding prompt messages and record detailed error logs.
[0071] Use the log grading mechanism to distinguish debug logs, warning logs, and error logs for subsequent analysis and optimization.
[0072] S5. Package into an executable program.
[0073] Allow users to execute manually as well, so that the program can be manually executed to achieve remote desktop exit without locking the screen.
[0074] Step S5 includes:
[0075] Package the Python script into an executable program and support both x86 and x64 architectures.
[0076] Write an installation program to simplify the user deployment process and support silent installation and custom installation paths.
[0077] Provide installation and usage instructions to ensure that users can get started quickly.
[0078] In this embodiment, by using Python to obtain the session ID of the current Windows user and using the tscon command to connect the session to the console (i.e., the desktop), the session can be restored more quickly and stably, reducing the time and operation complexity for users to re-login, improving work efficiency, and fully considering compatibility with the existing Windows operating system to ensure seamless operation on various network environments and hardware devices.
[0079] Specific implementation cases:
[0080] Case 1: Internal remote work within an enterprise
[0081] Background:
[0082] Employees within a certain enterprise access the company's internal server through remote desktop connection for daily work. Due to unstable network, the remote desktop connection often disconnects, resulting in the server automatically locking the screen and affecting work efficiency.
[0083] Implementation steps:
[0084] 1. Deploy the optimized Python script on the server to ensure that the remote desktop connection status can be monitored in real time.
[0085] 2. Configure the script parameters, set the session ID cache time and the retry times of the tscon command.
[0086] 3. Package the script into an executable program and deploy it through the enterprise internal installer.
[0087] 4. When employees access the server through remote desktop connection, the script monitors the connection status in real time. When it detects that the connection is disconnected, it automatically restores the session to prevent the server from locking the screen.
[0088] Effects:
[0089] 1. After the remote desktop connection is disconnected, the server no longer automatically locks the screen, and employees can continue with the unfinished work.
[0090] 2. Employees do not need to log in to the server again, saving time and improving work efficiency.
[0091] 3. The internal data processing of the enterprise is more fluent, reducing the risk of data loss caused by connection interruption.
[0092] Case 2: Remote operation and maintenance
[0093] Background:
[0094] The operation and maintenance personnel of a certain enterprise need to frequently access the server through remote desktop connection for operation and maintenance operations. Due to unstable network, the remote desktop connection often disconnects, resulting in the server automatically locking the screen and affecting the operation and maintenance quality.
[0095] Implementation steps:
[0096] 1. Deploy the optimized Python script on the server to ensure that the remote desktop connection status can be monitored in real time.
[0097] 2. Configure the script parameters, set the session ID cache time to 10 minutes, and set the number of retries for the tscon command to 5 times.
[0098] 3. Package the script into an executable program and deploy it through the enterprise internal installer.
[0099] 4. When the operation and maintenance personnel access the server through the remote desktop connection, the script monitors the connection status in real time. When it detects that the connection is disconnected, it automatically restores the session to prevent the server from locking the screen.
[0100] Effect:
[0101] 1. After the remote desktop connection is disconnected, the server no longer automatically locks the screen. The operation and maintenance personnel can continue with the unfinished operation and maintenance operations without having to log in again, saving time and improving the operation and maintenance efficiency.
[0102] 2. The operation and maintenance personnel can discover and solve problems more promptly, avoiding misjudgment of the server status caused by re-logging in, and improving the operation and maintenance quality.
[0103] 3. The status of the enterprise internal server is more stable, reducing the risk of misjudgment in operation and maintenance and server downtime caused by connection interruptions, and ensuring the continuity of business.
[0104] The above content is a further detailed description of the present invention in combination with specific preferred embodiments. It cannot be determined that the specific implementation of the present invention is only limited to these descriptions. For those of ordinary skill in the technical field to which the present invention belongs, without departing from the concept of the present invention, several simple deductions or substitutions can be made, and all should be regarded as belonging to the protection scope of the present invention.
Claims
1. A method for solving the problem of automatic screen lock after Windows remote desktop connection is disconnected, characterized in that: The method comprises the following steps: S1. Monitor the remote desktop connection status; S2. Get the user session ID; S3. Use the tscon command to restore the session. S4, error handling and logging; S5. Package into an executable program.
2. A method for solving the problem of automatic screen lock after Windows remote desktop connection is disconnected as claimed in claim 1, characterized in that: Step S1 includes: Perform real-time process monitoring and use Python scripts combined with Windows API to implement connection status monitoring; Perform a process scan at regular intervals. If no active session is found after three consecutive scans, the recovery mechanism is triggered. By parsing the entries related to remote desktop in the Windows event log and combining them with the process monitoring results, the connection status can be comprehensively judged.
3. A method for solving the problem of automatic screen lock after Windows remote desktop connection is disconnected as claimed in claim 1, characterized in that: Step S2 includes: Use getpass.getuser() to get the username of the current Windows user; Call subprocess.Popen to execute the query session command to obtain the current session information; Parse the current user's session ID from the command output and cache it.
4. A method for solving the problem of automatic screen lock after Windows remote desktop connection is disconnected as claimed in claim 1, characterized in that: Step S3 includes: Based on the obtained user session ID, the system command line is called through the Python script, and the tscon command is executed to reconnect the session to the console. Check the execution result of the tscon command and retry if it fails; Record the results of each command execution for subsequent analysis and optimization.
5. A method for solving the problem of automatic screen lock after Windows remote desktop connection is disconnected as claimed in claim 1, characterized in that: Step S4 includes: Add an error classification mechanism to the script to distinguish different error types; Provide corresponding prompt information for different error types and record detailed error logs; Use the log classification mechanism to distinguish debug logs, warning logs, and error logs.
6. A method for solving the problem of automatic screen lock after Windows remote desktop connection is disconnected as claimed in claim 5, characterized in that: The error types include disconnection, failure to obtain a session ID, and failure to execute a tscon command.
7. A method for solving the problem of automatic screen lock after Windows remote desktop connection is disconnected as claimed in claim 1, characterized in that: Step S5 includes: Package Python scripts into executable programs and support both x86 and x64 architectures; Write an installer to simplify the user deployment process and support silent installation and custom installation path; Provide installation and usage instructions to ensure users can get started quickly.