View Full Version : TASKLIST windows function
Hi All
sometimes i test examples which does not exit correctly, and the thinbasic.exe still running in the background. i don't like going to task manager with ctrl+alt+del and then searching the long list for thinbasic.exe . so googling and i find the solution:
http://superuser.com/questions/914782/how-do-you-list-all-processes-on-the-command-line-in-windows
from the command prompt:
TASKLIST /FI "IMAGENAME eq thinbasic.exe"
tskill thinbasic
look the picture below
this is tested in windows xp/32. not sure if the same apply to windows 7 and above
to test it run the first example here: http://www.thinbasic.com/community/showthread.php?11886-Mirage
as it is before the later corrections. and exit it by clicking 'X' not by pressing ESC. try it 3 times so 3 instances of thinbasic.exe in the memory.
9619
Petr Schreiber
01-08-2016, 18:52
What, what, whaaat? :D
Are there any SampleScripts that do that for you? Let us know and we will have a closer look.
Petr
ErosOlmi
01-08-2016, 20:45
It can happen when script has a Graphical User Interface (a window) and at some point there is an infinite loop.
Even if the user is able to close the window, the script is still executing the infinite loop.
It is good practice to always test, inside the infinite loop, if the main script window still exists.
In console scripts, usually closing the console window automatically close the process.
Anyway, any example showing the problem is welcome to try to find a fix.
Ciao
Eros
mike lobanovsky
01-08-2016, 21:37
Adding an explicit call to the ExitProcess(n) (https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658(v=vs.85).aspx) WinAPI (a kernel32.dll export), where n is the user-defined program exit code to be returned to the OS once the process is closed, to the program epilogue is an almost 100% foolproof method to achieve clean exit from a program of any complexity -- a multi-threaded application, a mixed GUI/CUI script, or an ordinary single-threaded GUI- or CUI-only program.
The call may be included by the developer into their interpreter's internal at-exit routine, and/or it can also be duplicated by the user in their particular script as the final statement in the application closing sequence, in which case the effect will be achieved by whatever call of the two (user or internal) will actually occur the first.
A rare case known to me that can still keep the interpreter core running is when the application spawns a sibling (not a child!) process, such as e.g. an MS Windows Help instance, via a call to ShellExecute() (https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx) or ShellExecuteEx() (https://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx) APIs (shell32.dll exports) in the interpreter or user sources that would keep shell32.dll running despite the exit process request. In this case, it is advisable that a call to TerminateProcess() (https://msdn.microsoft.com/en-us/library/windows/desktop/ms686714(v=vs.85).aspx) (yet another kernel32.dll export) be used instead of ExitProcess(n) in the end-user script, and/or the interpreter source code, and/or both, which is somewhat rougher but ensures the unconditional closing of all dependencies in the process to be terminated/aborted.
the sample to let thinbasic.exe stay in memory is like this:
Uses "ui"
DWord hwin
hwin=Canvas_Window("Canvas Test",1,1,640,480)
While Not GetAsyncKeyState(27)
'While Not GetAsyncKeyState(27) And IsWindow(hwin)
Canvas_Color(Rgb(255,255,255),0)
Canvas_SetPos(160,10)
Canvas_Print("test")
Canvas_Redraw
Wend
Canvas_Window End
click on 'X' and not pressing ESC key
in fact i feel immediately that thinbasic.exe stay in memory from the some lag in closing the window.
one day believe or not while doing too much testing and running too much scripts from other users i have about 10 thinbasic.exe until i begins to feel the system is too slow. naturaly i will press ctrl-del-alt to seel the list exe's
while in the previous forum link Eros suggested using
While Not GetAsyncKeyState(27) and IsWindow(hwin)
but in many cases the users forgot to write the correct way to exit
my solution is to make patch file such as remove.bat which have these lines
TASKLIST /FI "IMAGENAME eq thinbasic.exe"
tskill thinbasic
pause
fortunately tskill.exe will remove all instances of thinbasic , while if we use task mangaer it will end what we click on
ErosOlmi
01-08-2016, 22:19
Thanks for the example.
As you already described, to solve the problem, change While loop to something like:
While Not GetAsyncKeyState(27) And IsWindow(hwin)
...
Wend
In this way loop exit condition is determined by not pressing Esc and hWin is still on screen.
Without testing for the presence of the window, loop is still executing the While/Wend testing if Esc key is pressed.
ErosOlmi
01-08-2016, 22:24
There is no one single solution to this situation.
There could be many different windows on screen, some hidden some visible. Just closing one does not mean thinBasic has to assume script is finished.
Or windows could be created from main loop only when some events occur and then close them on a timer event but main loop must still run.
So, ... not easy to find a precise situation where the thinBASIC Core engine exactly knows when it has to shut down.