25
loading...
This website collects cookies to deliver better user experience
taskkill \IM %application_name% \F
where %application_name%
is the name of the application which we want to close. This command will forcefully close all instances of the application when the time is due. Sounds easy, right?import time as time_module # The time module
from threading import Thread #The threading module will allow us to run the program on multiple threads
from subprocess import Popen #This allows us to execute shell commands
class AppKiller:
"""
The code for our app
"""
def __init__(self, name, time):
self.name = name #The application's name
self.time = time #The time at which it is to close
def get_time(self):
#This function returns the current time using the time module
return str(time_module.strftime("%R"))
def watch(self):
#This is the function which does the killing of the application
#It runs an inner function which loops every two seconds to check
#if the current time is equal to the closing time of the application
#it runs a shell command which takes the application's name and then
#kills it
def inner_function():
while self.get_time() != self.time:
time_module.sleep(2)
command = Popen(["taskkill","/IM",self.name,"/F"],shell=True)
Thread(target=inner_function).start()
"""
Example
=======
brave = AppKiller("brave.exe","08:32")
brave.watch()
node_js = AppKiller("node.js","21:13")
node_js.watch()
"""
watch
method, there is an inner function which checks if the current time is equal to the end time of an app, if it is, it then kills the application, else, it sleeps for 2 seconds and checks again.08:30 (ie. 8:30 AM)
and Brave Browser will close at 21:21 (ie. 9:21PM)
. brave = AppKiller("brave.exe","21:21")
brave.watch()
node_js = AppKiller("node.exe","08:30")
node_js.watch()
21:21
comes before 08:30
. Since each instance of the AppKiller
class runs on a different thread, the time won't be affected, that is, brave.exe
will not close before node.exe
, they will all close when their time is due since they all run on different threads.