I wrote a program, program A, that runs another program, program B. If the program B crashes for some reason, Program A ends itself and restarts again to rerun program B properly.
I need to automatically set the process priority each time the program is rerun. Preferably through a batch file. However I cannot use the "start" command since it's simply not compatible.
Is there any way I can do this?
I can't use any 3rd party programs for this, since Program B is a server. And some of my clients use paid services for their servers.
Process Priority through batch files
Process Priority through batch files
▓▓▓▓▓▒▒▒▒▒░░░░░
Re: Process Priority through batch files
Here is some old experimental code I was using to set thread and process priority when I was experimenting with a game a while back. Maybe you can dig something out of this to get an idea. Reads info from a .ini file. It might be eaisly adaptable for your use. Not using a batch file, but a small change and you just read it from a .ini when your process starts.
Code: Select all
#THREAD_PRIORITY_NORMAL = 0
#THREAD_PRIORITY_ABOVE_NORMAL = 1
#THREAD_PRIORITY_HIGHEST = 2
#THREAD_PRIORITY_TIME_CRITICAL = 15
#ABOVE_NORMAL_PRIORITY_CLASS = $00008000
#HIGH_PRIORITY_CLASS = $00000080
#PROCESS_ALL_ACCESS_VISTA_WIN7 = $1FFFFF ; for win7 and Vista
;Global out_pDisableTPriorityBoost.i, Proc_PriorityBoost.i
Prototype.i PFNCreateToolhelp32Snapshot(dwFlags.i, th32ProcessID.i) ;
Prototype.b PFNProcess32First(hSnapshot.i, *lppe.PROCESSENTRY32) ;
Prototype.b PFNProcess32Next(hSnapshot.i, *lppe.PROCESSENTRY32) ;
Procedure GetPidByName(p_name$)
Protected hDLL.i, process_name$
Protected PEntry.PROCESSENTRY32, hTool32.i
Protected pCreateToolhelp32Snapshot.PFNCreateToolhelp32Snapshot
Protected pProcess32First.PFNProcess32First
Protected pProcess32Next.PFNProcess32Next
Protected pid.i
hDLL = OpenLibrary(#PB_Any,"kernel32.dll")
If hDLL
pCreateToolhelp32Snapshot = GetFunction(hDLL,"CreateToolhelp32Snapshot")
pProcess32First = GetFunction(hDLL,"Process32First")
pProcess32Next = GetFunction(hDLL,"Process32Next")
Else
ProcedureReturn 0
EndIf
PEntry\dwSize = SizeOf(PROCESSENTRY32)
hTool32 = pCreateToolhelp32Snapshot(#TH32CS_SNAPPROCESS, 0)
pProcess32First(hTool32, @PEntry)
process_name$ = Space(#MAX_PATH)
CopyMemory(@PEntry\szExeFile,@process_name$,#MAX_PATH)
If UCase(process_name$) = UCase(p_name$)
ProcedureReturn PEntry\th32ProcessID
EndIf
While pProcess32Next(hTool32, @PEntry) > 0
process_name$ = Space(#MAX_PATH)
CopyMemory(@PEntry\szExeFile,@process_name$,#MAX_PATH)
If UCase(process_name$) = UCase(p_name$)
ProcedureReturn PEntry\th32ProcessID
EndIf
Wend
CloseLibrary(hDLL)
ProcedureReturn 0
EndProcedure
Procedure.b CheckRunningExe(FileName.s)
Protected snap.i , Proc32.PROCESSENTRY32 , dll_kernel32.i
FileName = GetFilePart(FileName)
dll_kernel32 = OpenLibrary(#PB_Any, "kernel32.dll")
If dll_kernel32
snap = CallFunction(dll_kernel32, "CreateToolhelp32Snapshot",$2, 0)
If snap
Proc32\dwSize = SizeOf(PROCESSENTRY32)
If CallFunction(dll_kernel32, "Process32First", snap, @Proc32)
While CallFunction(dll_kernel32, "Process32Next", snap, @Proc32)
If PeekS(@Proc32\szExeFile)=FileName
CloseHandle_(snap)
CloseLibrary(dll_kernel32)
ProcedureReturn #True
EndIf
Wend
EndIf
CloseHandle_(snap)
EndIf
CloseLibrary(dll_kernel32)
EndIf
ProcedureReturn #False
EndProcedure
Procedure Elevated_Cmd(app_dir.s, app_name.s)
AppVerb$ = "runas"
AppName$ = app_name
AppDir$ = app_dir
shExecInfo.SHELLEXECUTEINFO
shExecInfo\cbSize=SizeOf(SHELLEXECUTEINFO)
shExecInfo\fMask=#Null
shExecInfo\hwnd=#Null
shExecInfo\lpVerb=@AppVerb$
shExecInfo\lpFile=@AppName$
shExecInfo\lpDirectory=@AppDir$
shExecInfo\nShow=#SW_NORMAL
exe.i = ShellExecuteEx_(shExecInfo)
Delay(3000)
If CheckRunningExe(app_name) = #True
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure Set_Priority()
Define te.THREADENTRY32
Protected out_pDisableTPriorityBoost.i, Proc_PriorityBoost.i
ini_dir$ = GetCurrentDirectory() + "gamepr.ini"
If FileSize(ini_dir$) = -1
MessageRequester("WARNING !!! ","gamepr.ini file does not exist !" + #CRLF$ + "Place valid gamepr.ini file in same folder as this application.")
End
EndIf
game_ini_rd$ = IniRead(ini_dir$,"Game","game_dir")
file_game$ = GetFilePart(game_ini_rd$)
path_game$ = GetPathPart(game_ini_rd$)
If (file_game$ = "") Or (path_game$ = "")
MessageRequester("Information","No valid path for game in gamepr.ini file !")
End
EndIf
If Elevated_Cmd(path_game$, file_game$) = #True
Delay(30000)
PID = GetPidByName(file_game$)
h=CreateToolhelp32Snapshot_(#TH32CS_SNAPTHREAD,0);
Lib_OpenThread = LoadLibrary_("kernel32.dll")
If Lib_OpenThread
*h_Thread = GetProcAddress_(Lib_OpenThread, "OpenThread")
FreeLibrary_(Lib_OpenThread)
EndIf
If (h<>#INVALID_HANDLE_VALUE)
te\dwSize=SizeOf(te)
If (Thread32First_(h,@te))
Repeat
If (te\dwSize>=OffsetOf(THREADENTRY32\th32OwnerProcessID)+SizeOf(te\th32OwnerProcessID))
If PID = te\th32OwnerProcessID
hProcess.i = OpenProcess_(#PROCESS_ALL_ACCESS_VISTA_WIN7,#False,te\th32OwnerProcessID)
hThread.i = CallFunctionFast(*h_Thread, #PROCESS_ALL_ACCESS_VISTA_WIN7,#False,te\th32ThreadID)
If hThread And hProcess
SetPriorityClass_(hProcess, #ABOVE_NORMAL_PRIORITY_CLASS)
SetProcessPriorityBoost_(hProcess, #False)
If GetThreadPriority_(hThread) = #THREAD_PRIORITY_NORMAL
SetThreadPriority_(hThread, #THREAD_PRIORITY_ABOVE_NORMAL)
SetThreadPriorityBoost_(hThread, #False)
EndIf
Debug "Process ID (PID) = " + Str(te\th32OwnerProcessID)
Debug "Process Handle (hProcess) = " + Str(hProcess) + " Process Priority = " + Str(GetPriorityClass_(hProcess))
Debug "Process Boost = " + Str(Proc_PriorityBoost)
GetProcessPriorityBoost_(hProcess, @Proc_PriorityBoost)
Debug "Process Boost Setting = " + Str(Proc_PriorityBoost)
Debug "Thread ID (TID) = " + Str(te\th32ThreadID)
Debug "Thread Handle (hThread) = " + Str(hThread) + " Thread Priority = " + Str(GetThreadPriority_(hThread))
Debug "Thread Boost = " + Str(Thrd_PriorityBoost)
GetThreadPriorityBoost_(hThread, @out_pDisableTPriorityBoost)
Debug "Thread Boost Setting = " + Str(out_pDisableTPriorityBoost)
EndIf
EndIf
EndIf
te\dwSize=SizeOf(te)
Until Not (Thread32Next_(h,@te));
EndIf
CloseHandle_(h)
EndIf
EndIf
EndProcedure
Set_Priority()
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
