| 
			
				|  | Cheat Engine The Official Site of Cheat Engine
 
 
 |  
 
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| HenryEx Expert Cheater
 
 ![]() Reputation: 2 
 Joined: 18 Dec 2011
 Posts: 100
 
 
 | 
			
				|  Posted: Tue Mar 04, 2025 9:25 am    Post subject: Problems using createThread() |   |  
				| 
 |  
				| Trying to fetch some game data asynchronously in the background without blocking the interface, and i'm having trouble with this. Most of the time, the UI still locks up while the thread runs. I've narrowed down my problem to two scenarios.
 
 Why does this freeze GUI until thread completes:
 
  	  | Code: |  	  | local function doSetupThread ( self, func, target, typ ) sleep(2000)
 print('Done')
 end
 
 local function setupAsync()
 createThread(doSetupThread())
 end
 
 setupAsync()
 | 
 
 
 And this doesn not freeze the GUI and runs in the background:
 
 
  	  | Code: |  	  | local function doSetupThread ( self, func, target, typ ) sleep(2000)
 print('Done')
 end
 
 local function setupAsync()
 createThread(doSetupThread)
 end
 
 setupAsync()
 | 
 
 These should be effectively identical.
 
 I don't want to write a separate function for all X threads i want to run, just one generalized function with certain arguments. But whenever i try to pass arguments via createThread (even if there are none), the thread creator seems to wait for the thread.
 |  |  
		| Back to top |  |  
		|  |  
		| Dark Byte Site Admin
 
  Reputation: 470 
 Joined: 09 May 2003
 Posts: 25807
 Location: The netherlands
 
 | 
			
				|  Posted: Tue Mar 04, 2025 9:56 am    Post subject: |   |  
				| 
 |  
				| In the first script you pass the result of doSetupThread  to createThread, which isn't a valid function (it returns nil)  so doesn't do anything 
 in the 2nd version you pass the function doSetupThread to create thread, which will result in the function being executed in another thread
 
 if you want to pass parameters do so by adding more parameters to createThread or use local vars (upvalues)
 
 
  	  | Code: |  	  | local upvalueexample=4
 
 local function doSetupThread ( self, func, target, typ )
 printf("%s %s %s", tostring(func), tostring(target), tostring(typ))
 sleep(2000)
 print('Done')
 printf("upvalueexample=%d",upvalueexample)
 end
 
 local function setupAsync()
 createThread(doSetupThread, 1,2,'3')
 end
 
 setupAsync()
 
 
 | 
 _________________
 
 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 |  |  
		|  |  
		| ParkourPenguin I post too much
 
  Reputation: 152 
 Joined: 06 Jul 2014
 Posts: 4706
 
 
 | 
			
				|  Posted: Tue Mar 04, 2025 10:52 am    Post subject: |   |  
				| 
 |  
				| More detail: 
  	  | Code: |  	  | function foo() return 5
 end
 
 print(('foo() = %d'):format(foo()))
 print(('foo   = %s'):format(foo))
 
 assert(foo() ~= foo)
 
 createthread(foo()) -- same as `createthread(5)`; bad
 createthread(foo) -- this is the one you want
 | 
 _________________
 
 I don't know where I'm going, but I'll figure it out when I get there. |  |  
		| Back to top |  |  
		|  |  
		| HenryEx Expert Cheater
 
 ![]() Reputation: 2 
 Joined: 18 Dec 2011
 Posts: 100
 
 
 | 
			
				|  Posted: Tue Mar 04, 2025 3:14 pm    Post subject: |   |  
				| 
 |  
				| I see how that works, now. I was a bit confused by the wiki entry on it, that clearly shows some ... arguments like this: 
 
  	  | Quote: |  	  | createThread(function(Thread,...), ...) | 
 
 I figured since it shows the thread passing itself down to the function in brackets, plus additional ... after that, that you need brackets if you want to send arguments down the line. The additional arguments shown after the brackets didn't help my confusion, but it sorta makes sense now.
 
 This is coming just in time, since i'll be wanting to use pcall() for my threads to make sure they clean up properly despite errors, and that works the same way!
 
 Thanks for the help.
 |  |  
		| 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 can download files in this forum
 
 |  |