View previous topic :: View next topic |
Author |
Message |
sgsgwv$6263 Advanced Cheater Reputation: 0
Joined: 05 Aug 2020 Posts: 75
|
Posted: Sat May 13, 2023 1:04 am Post subject: Access violation when trying to create timer inside a thread |
|
|
Whenever I try to create a timer inside a new thread in a script I get an access violation error message.
Is it possible to do what I am trying to do or the access violation might be for something else?
|
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 140
Joined: 06 Jul 2014 Posts: 4289
|
Posted: Sat May 13, 2023 10:46 am Post subject: |
|
|
Timers are related to the GUI. They need to be created and accessed in the main thread. Use `synchronize`
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
sgsgwv$6263 Advanced Cheater Reputation: 0
Joined: 05 Aug 2020 Posts: 75
|
Posted: Sat May 13, 2023 11:06 am Post subject: |
|
|
But that defeats the purpose of creating a seperate thread since it will block till the functions returns. But that function is creating a timer. What will happen to the timer after the function returns.
I am in a situation where I want the timer to run very fast like 50 milliseconds. But I also have a hotkey setup which because of the timer is (what I assume) reacting slowly. So i thought maybe creating a new thread will be beneficial. ChatGPt suggests using queue(). What do you think?
|
|
Back to top |
|
|
AylinCE Grandmaster Cheater Supreme Reputation: 32
Joined: 16 Feb 2017 Posts: 1253
|
Posted: Sat May 13, 2023 11:49 am Post subject: |
|
|
sgsgwv$6263 wrote: | What will happen to the timer after the function returns. |
Code: | timer.Destroy() -- timer = your timer name.. |
If your hotkey is slow to access, place another test in the existing code (Pass or fail, code value, changes in address, etc.) and the hotkey option can stay as a cookie next to them.
_________________
|
|
Back to top |
|
|
sgsgwv$6263 Advanced Cheater Reputation: 0
Joined: 05 Aug 2020 Posts: 75
|
Posted: Sat May 13, 2023 11:54 am Post subject: |
|
|
Got a workaround. Instead of creating an explicit timer I can implement a timer on the new thread with infinite while loops with sleep() in it for interval timing. Since this is not a cheat engine timer so will not cause access violation or block the main thread.
|
|
Back to top |
|
|
AylinCE Grandmaster Cheater Supreme Reputation: 32
Joined: 16 Feb 2017 Posts: 1253
|
Posted: Sat May 13, 2023 12:08 pm Post subject: |
|
|
Intermittent Sleep() and "Infinite Loop", that sounds more dangerous.
Consider a solution where you are in control.
But still, the decision is yours.
_________________
|
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 140
Joined: 06 Jul 2014 Posts: 4289
|
Posted: Sat May 13, 2023 3:01 pm Post subject: |
|
|
The separate thread is being used to avoid relying on pascal timers.
A while loop and a call to `sleep` in a separate thread is what should be done. Remember to check the Terminated property of the thread.
(even though the Lua interpreter is single threaded, CE doesn't need to lock a mutex to call `sleep` from Lua, so this is fine)
`sleep` shouldn't be called in the main thread to avoid freezing the GUI. In a separate thread, it's perfectly fine.
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
|