ahkThread and ComObjCreate

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
think
Posts: 136
Joined: 09 Feb 2014, 05:20

ahkThread and ComObjCreate

27 Sep 2021, 14:10

In some cases when ahkThread containing ComObjCreate is run for the second time, ahkTerminate is having issues to terminate it. See the example below - if CreateItem is done in two lines it doesn't work. I know this would be solved by adding m:="""" to the end of the string to release the variable.

But I also see that I could solve it using dll1.ahkTerminate(-1) to force the termination. My question is how ahkTerminate works and is there any potential problem using dll1.ahkTerminate(-1) or dll1.ahkTerminate(-500) or even dll1.ahkTerminate(-0) instead of dll1.ahkTerminate(). What is the safest way to terminate potentially problematic script? Thanks.

Code: Select all

string:="
(
#Persistent
;Doesn't work
mx:=ComObjCreate(""Outlook.Application"")
m:=mx.CreateItem(0)
;This works
;m:=ComObjCreate(""Outlook.Application"").CreateItem(0) 

m.Body:=""My body ...""
m.Display
return
)"

dll1:=ahkThread(string)
msgbox Try second time...
dll1.ahkTerminate()
dll1:=ahkThread(string)
return
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: ahkThread and ComObjCreate

27 Sep 2021, 17:20

Download latest version, should work fine now ;)
think
Posts: 136
Joined: 09 Feb 2014, 05:20

Re: ahkThread and ComObjCreate

28 Sep 2021, 02:30

Thanks. What about the general usage of ahkTerminate? Is ahkTerminate(-1) ok?
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: ahkThread and ComObjCreate

28 Sep 2021, 03:13

Negative number will terminate thread if it cannot exit after given time, in that case the thread will not have a chance to clear memory and such.
whynotregister
Posts: 147
Joined: 05 Nov 2016, 22:42

Re: ahkThread and ComObjCreate

28 Sep 2021, 03:51

This update causes an error related to com 0x800401E4.
It happens when combined with some code. I haven't been able to determine exactly under what circumstances it happens.
whynotregister
Posts: 147
Joined: 05 Nov 2016, 22:42

Re: ahkThread and ComObjCreate

28 Sep 2021, 05:48

HotKeyIt wrote:
28 Sep 2021, 05:21
Can you please try again now
Update to c++ 14 and fix COM << The error only occurs in that version.
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: ahkThread and ComObjCreate

28 Sep 2021, 05:51

So the latest one produces the error as well?
In that case I will need example code.
whynotregister
Posts: 147
Joined: 05 Nov 2016, 22:42

Re: ahkThread and ComObjCreate

28 Sep 2021, 05:59

error only occurs in the latest version.
I'll figure out the exact code causing the error and leave an answer.
whynotregister
Posts: 147
Joined: 05 Nov 2016, 22:42

Re: ahkThread and ComObjCreate

28 Sep 2021, 07:05

sent you a message.
think
Posts: 136
Joined: 09 Feb 2014, 05:20

Re: ahkThread and ComObjCreate

28 Sep 2021, 08:19

HotKeyIt wrote:
28 Sep 2021, 03:13
Negative number will terminate thread if it cannot exit after given time, in that case the thread will not have a chance to clear memory and such.
So if I understand correctly ahkTerminate(-100) waits 100ms before forcing the termination?
In case of, for example, ahkTerminate(200), what happens after 200ms?
And what's the default wait time of ahkTerminate()?
Any problem of forcing termination (not clearing the memory) in practice?
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: ahkThread and ComObjCreate

28 Sep 2021, 08:52

think wrote:
28 Sep 2021, 08:19
1. So if I understand correctly ahkTerminate(-100) waits 100ms before forcing the termination?
2. In case of, for example, ahkTerminate(200), what happens after 200ms?
3. And what's the default wait time of ahkTerminate()?
4. Any problem of forcing termination (not clearing the memory) in practice?
1. Yes, it also uses SMTO_NORMAL timeout instead of SMTO_NOTIMEOUTIFNOTHUNG to make sure that ahkTerminate succeeds.
2. If thread is hanging it will return after 200ms, otherwise it will wait due to SMTO_NOTIMEOUTIFNOTHUNG.
3. Wait forever
4. Depends what the thread does, beside not clearing memory it might not release objects (COM) and probably other problems.
think
Posts: 136
Joined: 09 Feb 2014, 05:20

Re: ahkThread and ComObjCreate

30 Sep 2021, 09:11

Thanks. The only thing I’m still not sure I understand - what’s the difference between ahkTerminate(100) and ahk Terminate(-100)?
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: ahkThread and ComObjCreate

30 Sep 2021, 09:54

-100 will force terminatation.
100 will:
- wait for thread exit (if thread is not hung and cannot exit it will wait forever)
- return if thread exited
- return if thread is hung and cannot exit after 100ms

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 16 guests