56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package main
|
|
|
|
/*
|
|
#include <windows.h>
|
|
BOOL SetPriority(DWORD pid, int priority) {
|
|
HANDLE h = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
|
|
if (!h) return FALSE;
|
|
BOOL success = SetPriorityClass(h, priority);
|
|
CloseHandle(h);
|
|
return success;
|
|
}
|
|
*/
|
|
import "C"
|
|
import (
|
|
"github.com/postfinance/single"
|
|
"github.com/shirou/gopsutil/process"
|
|
"os"
|
|
"runtime"
|
|
"syscall"
|
|
"time"
|
|
"unsafe"
|
|
)
|
|
|
|
func MessageBox(lpText string, lpCaption string, uType int) int {
|
|
var convertedCaption, _ = syscall.UTF16PtrFromString(lpCaption)
|
|
var convertedTitle, _ = syscall.UTF16PtrFromString(lpText)
|
|
returnCode, _, _ := syscall.NewLazyDLL("user32.dll").NewProc("MessageBoxW").Call(
|
|
uintptr(0),
|
|
uintptr(unsafe.Pointer(convertedCaption)),
|
|
uintptr(unsafe.Pointer(convertedTitle)),
|
|
uintptr(uType),
|
|
)
|
|
return int(returnCode)
|
|
}
|
|
|
|
var lockFile, _ = single.New("autoPriority")
|
|
|
|
func main() {
|
|
if err := lockFile.Lock(); err != nil {
|
|
MessageBox("The program is already running", "Only one instance of the program can be run\nYou can close it via the Task Manager", 48)
|
|
os.Exit(1)
|
|
}
|
|
|
|
C.SetPriority(C.DWORD(os.Getpid()), 0x00000040)
|
|
for range time.Tick(time.Minute) {
|
|
processes, _ := process.Processes()
|
|
for _, proc := range processes {
|
|
memoryInfo, err := proc.MemoryInfo()
|
|
if err == nil && memoryInfo.RSS >= (0.5*1024*1024*1024) {
|
|
C.SetPriority(C.DWORD(proc.Pid), 0x00000080)
|
|
}
|
|
}
|
|
runtime.GC()
|
|
}
|
|
}
|