# invokeAsynchronous

**URL:** <https://forum.inductiveautomation.com/t/invokeasynchronous/4303>\
**Category:** Ignition\
**Created:** [April 17, 2012, 1:13pm UTC](https://forum.inductiveautomation.com/t/invokeasynchronous/4303 "2012-04-17T13:13:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Step7](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Step7](https://forum.inductiveautomation.com/u/Step7)\
**Post date:** [April 17, 2012, 1:13pm UTC](https://forum.inductiveautomation.com/t/invokeasynchronous/4303/1 "2012-04-17T13:13:34Z")

</div>

I’ve got a question about my old nemisis, the invokeAsynchronous call. Here’s what I’m trying to do: I have another function that is listening for incoming messages, and it adds the raw data to a dictionary. In this new function, I just want it to sit there spinning and check to see if there are new values in the dictionary, do something with it, and then delete the new value. Right now I’m doing it with a timer, but it really isn’t the right way.

Here’s my pseudo code:

```auto
def doAsynch():
	def copyData():
		global myDictionary
		while 1:
			k = myDictionary.keys() 
			k.sort()
			for i in k::
				#Do a bunch of stuff here
				del myDictionary[i]
	copyData()
system.util.invokeAsynchronous(doAsynch)
```

I do something very similar in my socket function, and it works fine, but this code locks up the GUI in the ‘while’ loop. Btw, this code is in the button press event.

Ideas on how to accomplish this? Thanks!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 17, 2012, 1:16pm UTC](https://forum.inductiveautomation.com/t/invokeasynchronous/4303/2 "2012-04-17T13:16:23Z")

</div>

Not to sound more ignorant than usual, but what breaks it out of the while loop?

---

<div class="post-metadata">

**Author:** ![Step7](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Step7](https://forum.inductiveautomation.com/u/Step7)\
**Post date:** [April 17, 2012, 1:28pm UTC](https://forum.inductiveautomation.com/t/invokeasynchronous/4303/3 "2012-04-17T13:28:34Z")

</div>

That’s the thing- I don’t want it to break out of the while loop. I just want it to sit there and constantly check if there are new values in the dictionary, just as my socket function works.

I thought by using invokeAsynchronous, it would run on its own thread and let the rest of the gui go on as usual, but I’m missing something.

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [April 17, 2012, 3:17pm UTC](https://forum.inductiveautomation.com/t/invokeasynchronous/4303/4 "2012-04-17T15:17:30Z")

</div>

It is locking up the GUI because of the while loop. You are spiking the CPU since the code is so fast. Your socket code works fine because it blocks and doesn’t run really fast. You either need to add a sleep inside of the loop or use a timer event script. To add a sleep do this[code]def doAsynch():  
def copyData():  
import time  
global myDictionary  
while 1:  
k = myDictionary.keys()  
k.sort()  
for i in k::  
#Do a bunch of stuff here  
del myDictionary[i]

```
     time.sleep(2) # sleep for 2 seconds

```

copyData()  
system.util.invokeAsynchronous(doAsynch)[/code]It is recommended to use a timer event script so you can remove the while loop. They run all the time in each client (if client side).
