|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
GH*master Expert Cheater Reputation: 8
Joined: 10 Jan 2008 Posts: 159
|
Posted: Sat Aug 20, 2011 2:58 am Post subject: Tutorial: Thread Class (SDK r1009, CE 6.2 PreRelease) |
|
|
Tutorial: Thread Class (SDK r1009, CE 6.2 PreRelease)
Information from CE SDK r1009:
Code: |
Thread Class: (Inheritance: Object)
createNativeThread(function) :
Executes the given function in another thread using the systems thread mechanism
The function returns the Thread class object
function declaration: function (Thread)
thread_freeOnTerminate(thread, state) :
When set to true the thread object will free itself when the function ends (default=true)
Note: Use this only from inside the thread function as the thread might have already terminated and freed itself when called
thread_synchronize(thread, function) :
Called from inside the thread. This wil cause the tread to get the main thread to execute the given function and wait for it to finish.
Usually for gui access
function ()
thread_waitfor(thread) : Waits for the given thread to finish
|
Tutorial: Thread Class
Code: |
trainerForm = createForm()
label1 = createLabel(trainerForm)
progressbar = createProgressBar(trainerForm)
label2 = createLabel(trainerForm)
btnExit= createButton(trainerForm)
control_setCaption(trainerForm, "CaptionDefault")
control_setCaption(label1, "CaptionDefault")
control_setCaption(label2, "CaptionDefault")
control_setCaption(btnExit, "Exit")
control_setSize(progressbar, 280, 20)
progressbar_stepBy(progressbar, 1)
progressbar_setMax(progressbar, 40000)
function CloseForm(sender)
form_close(trainerForm)
trainerForm = nil
end
control_onClick(btnExit, CloseForm)
x = 20
y = 10
dy = 20
control_setPosition(label1, x, y+1*dy)
control_setPosition(progressbar, x, y+2*dy)
control_setPosition(label2, x, y+3*dy)
control_setPosition(btnExit, x + 220, y+10*dy)
form_centerScreen(trainerForm)
----------Main Part ---------------
j = 0
function TestThread1(senderThread)
for i=1,40000 do
j = j + 1
control_setCaption(label1, ' "thread1" have while many times '..j)
progressbar_setPosition(progressbar, j)
end
end
thread1 = nil
function TestThread2(senderThread)
control_setCaption(label2, "Wait...")
thread_synchronize(senderThread,thread_waitfor(thread1))
control_setCaption(label2, "Done!")
end
thread1 = createNativeThread(TestThread1)
thread2 = createNativeThread(TestThread2)
---------- End Main Part ---------------
|
|
|
Back to top |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25288 Location: The netherlands
|
Posted: Sat Aug 20, 2011 3:48 am Post subject: |
|
|
tip: Generally speaking, do not use gui calls inside threads as most of them are not thread safe. (setCaption and the progressbar are thread safe, but it of course completly destroys any reason for threads as they are automatically synchronized with the main thread)
I recommend making use of global variables and the synchronize variable instead and using a timer to update the screen
Also, thread 1 might have been destroyed by the time thread 2 even gets to execute causing thread_waitfor to crash. To prevent that set FreeOnTerminate to false in the thread which has just been created
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
|
GH*master Expert Cheater Reputation: 8
Joined: 10 Jan 2008 Posts: 159
|
Posted: Sat Aug 20, 2011 7:56 am Post subject: |
|
|
Ok
Code: |
....
----------Main Part ---------------
j = 0
function onTimer(senderTimer)
timer_setEnabled(timer, false)
control_setCaption(label2, "Wait...")
for i=1,40000 do
j = j + 1
control_setCaption(label1, ' "timer" have while many times '..j)
progressbar_setPosition(progressbar, j)
end
control_setCaption(label2, "Done!")
end
timer = createTimer(trainerForm)
timer_setInterval(timer, 1000)
timer_onTimer(timer, onTimer)
---------- End Main Part ---------------
|
|
|
Back to top |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25288 Location: The netherlands
|
Posted: Sat Aug 20, 2011 8:55 am Post subject: |
|
|
I was thinking more in the way of:
Code: |
j = 0
function finished(self)
--executed from the main thread
control_setCaption(label2, "Done!")
timer_setEnabled(timer, false)
onTimer(timer) --one last update
end
function WorkerThread(self)
local i=0;
for i=1,40000 do
j = j + 1
sleep(3) --just as example so you see what happens
end
thread_synchronize(self, finished)
end
function onTimer(senderTimer)
control_setCaption(label1, ' "timer" have while many times '..j)
progressbar_setPosition(progressbar, j)
end
control_setCaption(label2, "Wait...")
timer = createTimer(trainerForm)
timer_setInterval(timer, 100)
timer_onTimer(timer, onTimer)
createNativeThread(WorkerThread)
|
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
|
GH*master Expert Cheater Reputation: 8
Joined: 10 Jan 2008 Posts: 159
|
Posted: Sat Aug 20, 2011 12:09 pm Post subject: |
|
|
Excellent!
|
|
Back to top |
|
|
ablonevn Advanced Cheater Reputation: 1
Joined: 02 Oct 2011 Posts: 59
|
Posted: Fri Jan 11, 2013 2:35 am Post subject: |
|
|
GH*master wrote: | Ok
Code: |
....
----------Main Part ---------------
j = 0
function onTimer(senderTimer)
timer_setEnabled(timer, false)
control_setCaption(label2, "Wait...")
for i=1,40000 do
j = j + 1
control_setCaption(label1, ' "timer" have while many times '..j)
progressbar_setPosition(progressbar, j)
end
control_setCaption(label2, "Done!")
end
timer = createTimer(trainerForm)
timer_setInterval(timer, 1000)
timer_onTimer(timer, onTimer)
---------- End Main Part ---------------
|
|
uhm, i can not find any example about using shared data between threads in 4r, eg: an example about producer & consumer, could you make one ? please,
|
|
Back to top |
|
|
Dark Byte Site Admin Reputation: 458
Joined: 09 May 2003 Posts: 25288 Location: The netherlands
|
Posted: Fri Jan 11, 2013 4:38 am Post subject: |
|
|
This is such an example
Testthread1 sets the position variable and the timer (that runs in a different thread) reads the position
Anyhow, lua encloses every single variable type in a global critical section, so no thread will ever change any variable at the same time as another one (even if it isn't the same var)
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
|
|
|
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 cannot download files in this forum
|
|