Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


(LUA) Thread class in CE.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Nov 20, 2018 2:06 pm    Post subject: (LUA) Thread class in CE. Reply with quote

Problem: my trainer becomes laggy if I set a timer and do several checks like this. I think it's related to the "getProcessIDFromProcessName" function:
Code:

if getProcessIDFromProcessName("xxx.exe") ~= nil or getProcessIDFromProcessName("aaa.exe") ~= nil then
    FormClose()
end


If the timer has a two-second interval, my trainer lags every two-second.

I have tried to use the "Thread" class, but it didn't work, the lag was still there.
Code:

function check()
   if getProcessIDFromProcessName("xxx.exe") ~= nil or getProcessIDFromProcessName("aaa.exe") ~= nil then
      FormClose()
   end
end

createThread(check())

--this doesn't work


Is there a way to fix this?

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 150

Joined: 06 Jul 2014
Posts: 4657

PostPosted: Tue Nov 20, 2018 3:00 pm    Post subject: Reply with quote

That's not how Lua works. You're invoking the check function and passing the result (nothing) to createThread, which I hope will gracefully fail and not cause any memory leaks.

I can't post example code because cloudfare thinks it's malicious, but there are plenty of examples using createThread if you search for them.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Nov 20, 2018 4:07 pm    Post subject: Reply with quote

ParkourPenguin wrote:
That's not how Lua works. You're invoking the check function and passing the result (nothing) to createThread, which I hope will gracefully fail and not cause any memory leaks.

I can't post example code because cloudfare thinks it's malicious, but there are plenty of examples using createThread if you search for them.


Thanks, ParkourPenguin.
I have two follow-up questions:

1. So I assume that using "createThread' can solve my problem, right?

2. If so, is this link helpful or pointing at the correct direction about how to use "createThread"?
https://forum.fivem.net/t/multiple-citizen-createthread-function-in-one-lua/102684

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 150

Joined: 06 Jul 2014
Posts: 4657

PostPosted: Tue Nov 20, 2018 4:48 pm    Post subject: Reply with quote

1. Yes; it'll stop the GUI from freezing.

2. I don't know what that is. Why not just search through CEF?

Maybe I can figure out how to not anger cloudfare.
Code:
if mytimer then mytimer.terminate(); mytimer = nil end

mytimer = createThread(function(thread)
  while not thread.Terminated do
    -- execute code here; don't forget to sleep
  end
end)

Edit: yup, apparently it doesn't like it when people call sleep.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Nov 20, 2018 4:59 pm    Post subject: Reply with quote

ParkourPenguin wrote:
1. Yes; it'll stop the GUI from freezing.

2. I don't know what that is. Why not just search through CEF?

Maybe I can figure out how to not anger cloudfare.
Code:
if mytimer then mytimer.terminate(); mytimer = nil end

mytimer = createThread(function(thread)
  while not thread.Terminated do
    -- execute code here; don't forget to sleep
  end
end)

Edit: yup, apparently it doesn't like it when people call sleep.


Thanks again. So does the follow code makes sense?
Code:

if mytimer then mytimer.terminate(); mytimer = nil end

mytimer = createThread(function(thread)
  while not thread.Terminated do
    if getProcessIDFromProcessName("xxx.exe") ~= nil or getProcessIDFromProcessName("aaa.exe") ~= nil then
      FormClose()
    end
  end
end)


Don't know what sleep means, I will do some research.

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 150

Joined: 06 Jul 2014
Posts: 4657

PostPosted: Tue Nov 20, 2018 5:13 pm    Post subject: Reply with quote

Calling sleep tells the OS that this thread doesn't need to do any work for a specified amount of time. Your code doesn't do that, so it'll always be running and wasting CPU time needlessly.

At the very end, just before the while loop terminates, call sleep and pass a value like 2000 (2 seconds).

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Nov 20, 2018 6:29 pm    Post subject: Reply with quote

ParkourPenguin wrote:
Calling sleep tells the OS that this thread doesn't need to do any work for a specified amount of time. Your code doesn't do that, so it'll always be running and wasting CPU time needlessly.

At the very end, just before the while loop terminates, call sleep and pass a value like 2000 (2 seconds).


So, should it look like this?
Code:

if mytimer then mytimer.terminate(); mytimer = nil end

mytimer = createThread(function(thread)
  while not thread.Terminated do
    if getProcessIDFromProcessName("xxx.exe") ~= nil or getProcessIDFromProcessName("aaa.exe") ~= nil then
      sleep(2000)
      FormClose()
    end
  end
end)

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 150

Joined: 06 Jul 2014
Posts: 4657

PostPosted: Tue Nov 20, 2018 6:47 pm    Post subject: Reply with quote

ParkourPenguin wrote:
At the very end, just before the while loop terminates...

Code:
while not thread.Terminated do
  if getProcessIDFromProcessName("xxx.exe") ~= nil or getProcessIDFromProcessName("aaa.exe") ~= nil then
    FormClose()
  end
  sleep(2000)
end

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Nov 20, 2018 7:52 pm    Post subject: Reply with quote

ParkourPenguin wrote:
ParkourPenguin wrote:
At the very end, just before the while loop terminates...

Code:
while not thread.Terminated do
  if getProcessIDFromProcessName("xxx.exe") ~= nil or getProcessIDFromProcessName("aaa.exe") ~= nil then
    FormClose()
  end
  sleep(2000)
end


Oh, got it. Thanks for the help, ParkourPenguin. I will give you rep when I can, Very Happy

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1588

PostPosted: Wed Nov 28, 2018 9:39 am    Post subject: Reply with quote

Hope this example will help, if I ever manage to post it.
thanks cloudflare Confused
Code:
print('start');
local stop,done,ts,sp = false,false,os.clock(),_G.sleep; -- variables
thread = createThreadSuspended(function (thread)
   while (true) do
      if (not done) then
         if (not (getProcessIDFromProcessName("xxx.exe") or getProcessIDFromProcessName("aaa.exe"))) then
            stop = true;
            break;
         end
         done = true;
      end
      sp(1); -- reduces execssive cpu usage and problems;
   end
end);
local i = 0; -- for counting and example only
timer = createTimer();
timer.interval = 100;
timer.onTimer = function (sender)
   if (os.clock() > ts + 2) then -- checks if 2 seconds passed as this function called 10 times
      thread.resume(); -- resumes our infinite loop
      ts = os.clock(); -- reset timestamp
   end
   if (stop) then -- its better to exit from main thread
      print('error game was not found - program should terminate');
      timer.destroy();
      -- return os.exit(); -- force app to exit
   end
   if (done) then -- our loop finished an action and can be put to sleep until the next action
      thread.suspend(); -- suspend our thread
      done = false; -- reset the flag only after suspend
      i = i + 1;
      print('action finished successfully', i, ' times');
   end
end

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites