本帖最后由 small-q 于 2019-1-31 10:46 编辑
进程守护,你请保安(for)来守护,那是要发工资,要消耗CPU的算力的.
我CPU比较好,看不出啥来!
[Golang] 纯文本查看 复制代码 for{
fmt.Println("老板,帮你看门,请发工资!")//不停的循环
}
现在有什么解决方案呢,不发工资,又能帮我看门的好方法? 下面是福利代码!
[Golang] 纯文本查看 复制代码 import (
"log"
"github.com/robfig/cron"
)
func (p *program) run() {
// 代码写在这儿
//for{//弃用for
c := cron.New()
c.AddFunc("@every 0.1m", func() {
exPath, _ := os.Executable() //获取当前程序运行目录
dir := filepath.Dir(exPath)
kernel32 := syscall.NewLazyDLL("kernel32.dll")
CreateToolhelp32Snapshot := kernel32.NewProc("CreateToolhelp32Snapshot")
pHandle, _, _ := CreateToolhelp32Snapshot.Call(uintptr(0x2), uintptr(0x0))
if int(pHandle) == -1 {
return
}
Process32Next := kernel32.NewProc("Process32Next")
for {
var proc PROCESSENTRY32
proc.dwSize = ulong(unsafe.Sizeof(proc))
if rt, _, _ := Process32Next.Call(uintptr(pHandle), uintptr(unsafe.Pointer(&proc))); int(rt) == 1 {
if strings.Contains(fmt.Sprintf("%s", string(proc.szExeFile[0:])), "SmallQExternal.exe") == true {
break
}
} else {
cmd := exec.Command(dir + "/小Q屏幕截图加载器.exe")
_, err := cmd.Output()
if err != nil {
//fmt.Printf("err: %v", err)
//return
} else {
//fmt.Printf("output: %s\n", buf)
}
break
}
}
CloseHandle := kernel32.NewProc("CloseHandle")
_, _, _ = CloseHandle.Call(pHandle)
})
c.Start()
//}
}
想到没? 用时钟! 请再别舍舍的用 for 啦! 感谢老飘与C版,让我看到用Go去创造完美世界!
|