# Problem With invokeAsynchronous

**URL:** <https://forum.inductiveautomation.com/t/problem-with-invokeasynchronous/5742>\
**Category:** Ignition\
**Created:** [January 24, 2013, 8:56pm UTC](https://forum.inductiveautomation.com/t/problem-with-invokeasynchronous/5742 "2013-01-24T20:56:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [January 24, 2013, 8:56pm UTC](https://forum.inductiveautomation.com/t/problem-with-invokeasynchronous/5742/1 "2013-01-24T20:56:44Z")

</div>

I am currently trying to create a script that opens an image that the user selects. It will then take this image and attach it to an email that is sent out. I’ve got it so that the image loads and the email is sent out. However, whenever it does this it locks up the GUI. I’ve even added in invokeAsynchronous to the script but it will still lock up the GUI and I am confused as to why. The CPU usage never goes above 30% when this script is running (generally around 10%) and I can use other programs while it is running with no problem. Below is my code and I have it set to run when a button is clicked. Any help would be greatly appreciated.

-Will

`if system.tag.getTagValue("[System]Client/User/Username") in ["user1", "user2"]:
	path = system.file.openFile()		
	if path != None:
		def longProcess(path = path):
			import system
			bytes = system.file.readFileAsBytes(path)
			def afterLongProcess(bytes = bytes):
				import system
				smtp = "smtp.gmail.com:587:tls"
				fromAddr = "sender@address.com"
				subject = "Subject"
				body = "Body"
				recipients = ["email@address.com"]
				fileData = bytes
				fileName = "Image_1.png"
				password = "password"
				system.net.sendEmail(smtp, fromAddr, subject, body, 0, recipients, [fileName], [fileData], 60000, fromAddr, password)
				system.gui.messageBox("Email was successfully sent.", "Email Complete")
			system.util.invokeLater(afterLongProcess)
		system.util.invokeAsynchronous(longProcess)
	else:
		system.gui.warningBox("No Image Selected", "Warning")`

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 24, 2013, 9:09pm UTC](https://forum.inductiveautomation.com/t/problem-with-invokeasynchronous/5742/2 "2013-01-24T21:09:42Z")

</div>

Everything in your afterLongProcess() call runs in the UI thread - have you checked to see if system.net.sendEmail() is blocking for any amount of time?

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [January 24, 2013, 9:17pm UTC](https://forum.inductiveautomation.com/t/problem-with-invokeasynchronous/5742/3 "2013-01-24T21:17:43Z")

</div>

Kevin - Never thought of that. Always assumed since the code was inside the asynchronous function that it would run the sendEmail as part of the asynchronous thread. Would there be any problem with taking the sendEmail part out of the invokeLater and running it in the asychronous thread?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 24, 2013, 9:20pm UTC](https://forum.inductiveautomation.com/t/problem-with-invokeasynchronous/5742/4 "2013-01-24T21:20:00Z")

</div>

That’s probably the right approach.

As it is, you’re explicitly asking afterLongProcess() to be run on the UI thread by calling system.util.invokeLater().

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [January 24, 2013, 9:26pm UTC](https://forum.inductiveautomation.com/t/problem-with-invokeasynchronous/5742/5 "2013-01-24T21:26:13Z")

</div>

Thanks a bunch Kevin! That did the trick.
