1

added a check for a running instance of the program

This commit is contained in:
2023-01-15 16:08:01 +03:00
parent 39030102b4
commit 0b714dd3ef
3 changed files with 32 additions and 2 deletions

View File

@@ -12,15 +12,37 @@ BOOL SetPriority(DWORD pid, int priority) {
*/
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 {
for range time.Tick(time.Minute) {
processes, _ := process.Processes()
for _, proc := range processes {
memoryInfo, err := proc.MemoryInfo()
@@ -29,6 +51,5 @@ func main() {
}
}
runtime.GC()
time.Sleep(time.Minute)
}
}