Posts: 795
Threads: 136
Joined: Feb 2009
Hi Gintaras,
just for clarification
if a macro is this
function1
function2
function3
then all functions are executed one after the other as planned.
but this
mac "function1"
function2
function3
makes function2 executed before function1
Why??
Thanks
Posts: 863
Threads: 197
Joined: Apr 2005
I think:
mac "function1" ----> function1 runs asynchronously, in separate thread.
Posts: 12,140
Threads: 142
Joined: Dec 2002
Yes. After creating new thread, if Windows does not interrupt current thread, new thread probably starts several microseconds or milliseconds later, and current thread can execute function2 and function3 during that time.
Posts: 795
Threads: 136
Joined: Feb 2009
OK. Was indeed a synchronisation delay problem.
I see at least two possible use of the mac "function" thing then:
1. launch a separate thread for monitoring something, and acting until a condition is met
exemple :
rep
if (some conditionis true) break
else do something else
0.1 (repeat endlessly til condition met)
In that case, will macro from where mac "function" is called ended whatever mac "function" status is?
2. launch a background task (if question above is yes)
Is it legitamate to act like this. If not, what are correct ways to do 1 & 2?
Thanks
Posts: 12,140
Threads: 142
Joined: Dec 2002
1. Both threads are independent from each other. If one of them wants to wait for another to end etc, need to add some code for this.
2. Yes.
Posts: 795
Threads: 136
Joined: Feb 2009
OK...some hints needed...
Say I have a macro M1 doing file conversion from context menu.
When function is invoked form context menu in explorer, function reads a file to determine what to convert.
If converting function not launched, launches it
If converting function already in use, feeds the queue file and launch it again when done with new queue file.
(i just need the framework with the correct keywords to use)
Thanks!!!
Posts: 12,140
Threads: 142
Joined: Dec 2002
Macro M1
Trigger $sm "Test convert"
;\
function $files
str f
foreach f files
,out f
,mac "sub.Convert" "" f
#sub Convert
function $f
lock
mes F"converting {f}"
Posts: 795
Threads: 136
Joined: Feb 2009
Great, the lock intruction does what needed.
But it is new that function don't change QM tray icon color while running, as seen in changelog.
do you use mac "sub.Convert" instead of sub.Convert to allow asynchro running?
If I want to stop process, what is the correct way (insert a "wait for key" function launched by mac "function")??
Posts: 12,140
Threads: 142
Joined: Dec 2002
Function threads don't change tray icon. You can use AddTrayIcon.
Quote:do you use mac "sub.Convert" instead of sub.Convert to allow asynchro running?
Yes, it executes #sub Convert in new thread.
Quote:If I want to stop process, what is the correct way
If want to have a hotkey to end a thread, you can create new function that calls EndThread, and assign a hotkey trigger.
Macro M1
Trigger $sm "Test convert"
;\
function $files
str f
foreach f files
,out f
,mac "sub.Convert" "" f
#sub Convert
function $f
lock
AddTrayIcon "$qm$\copy.ico" F"converting {f}"
mes F"converting {f}"
Function end_convert
Trigger CAe
;Ctrl+Alt+E
EndThread "M1:Convert"
Or right click or Ctrl+click in Running items. Or use QM tray icon menu -> Threads.
Or Ctrl+click tray icon added by AddTrayIcon. But it does not work if the thread does not process messages.
Posts: 795
Threads: 136
Joined: Feb 2009
yes, i use that elsewhere, but i'd like the stop procedure to be unique and only for that procedure (file conversion)
If I implement it as you say, can i call it
using that code?
function $files
str f
foreach f files
,out f
,mac "sub.Convert" "" f
,mac "end_convert"
#sub Convert
.....
Posts: 12,140
Threads: 142
Joined: Dec 2002
Yes, then let end_convert wait for a key etc before EndThread.
Macro M1
Trigger $sm "Test convert"
;\
function $files
str f
foreach f files
,out f
,mac "sub.Convert" "" f
,mac "sub.End"
#sub Convert
function $f
lock
AddTrayIcon "$qm$\copy.ico" F"converting {f}"
mes F"converting {f}"
#sub End
wait 0 K C ;;Ctrl
EndThread "M1:Convert"
Not tested with multiple waiting threads. Probably will need more code.
Posts: 12,140
Threads: 142
Joined: Dec 2002
This version does not kill waiting threads.
Macro M1
Trigger $sm "Test convert"
;\
function $files
str f
foreach f files
,out f
,mac "sub.Convert" "" f
#sub Convert
function $f
lock
QMTHREAD qt; GetQmThreadInfo 0 &qt
mac "sub.End" "" qt.threadhandle
AddTrayIcon "$qm$\copy.ico" F"converting {f}"
mes F"converting {f}"
#sub End
function ht
rep
,wait 1 K C ;;Ctrl
,err
,,if(WaitForSingleObject(ht 0)!=WAIT_TIMEOUT) break
,,continue
,EndThread "" ht
,break
Posts: 795
Threads: 136
Joined: Feb 2009
both work well, maybe second version is safer.
As usual, thanks.
Posts: 795
Threads: 136
Joined: Feb 2009
oh just a last thing, how to get the position of the systray icon, by using it's windows handle?
i'd like to use TrayToolTip macro with it....
Posts: 12,140
Threads: 142
Joined: Dec 2002
It is difficult, undocumented, although I have a function.
Better find by tooltip. Use accessible object functions.
Or simply show OSD with OnScreenDisplay in right-bottom corner.
Posts: 795
Threads: 136
Joined: Feb 2009
ok, i'll forget that...
About the third version usint GetQmThreadInfo, if some call are in queue due to lock feature, waiting to be processed, which version ensures that a Ctrl press will abort all
conversions, current and queued ones?
Posts: 795
Threads: 136
Joined: Feb 2009
Gintaras Wrote:Or simply show OSD with OnScreenDisplay in right-bottom corner.
Due to difficulties of color scheme and borders, i you have a macro that makes an OSD look like a tooltip, i'm interested...
Posts: 12,140
Threads: 142
Joined: Dec 2002
This version correctly ends all threads.
Macro M1
Trigger $sm "Test convert"
;\
function $files
int+ g_convertEndingThreads
str f
foreach f files
,out f
,mac "sub.Convert" "" f
,if(!IsThreadRunning("M1:End")) mac "sub.End"
#sub Convert
function $f
lock
if(g_convertEndingThreads) ret
AddTrayIcon "$qm$\copy.ico" F"converting {f}"
mes F"converting {f}"
#sub End
rep
,wait 1 K C ;;Ctrl
,err
,,if(!IsThreadRunning("M1:Convert")) break
,,continue
,g_convertEndingThreads=1
,EndThread "M1:Convert" 0 8
,g_convertEndingThreads=0
,break
Posts: 12,140
Threads: 142
Joined: Dec 2002
Quote:Due to difficulties of color scheme and borders, i you have a macro that makes an OSD look like a tooltip
System tooltip colors for text and background. Also possible to get system tooltip font name and size.
Macro Macro2499
int col1=GetSysColor(COLOR_INFOTEXT); if(!col1) col1=1
int col2=GetSysColor(COLOR_INFOBK); if(!col2) col2=1
OnScreenDisplay "Text" 3 0 0 "" 9 col1 5 "" col2
Posts: 795
Threads: 136
Joined: Feb 2009
Gintaras Wrote:Also possible to get system tooltip font name and size.
yes, please do...
Posts: 12,140
Threads: 142
Joined: Dec 2002
Macro Macro2501
NONCLIENTMETRICS m.cbSize=sizeof(m)
SystemParametersInfo(SPI_GETNONCLIENTMETRICS m.cbSize &m 0)
LOGFONT& p=&m.lfStatusFont
int hdc=GetDC(0); out -MulDiv(p.lfHeight 72 GetDeviceCaps(hdc LOGPIXELSY)); ReleaseDC 0 hdc
str s.fromn(&p.lfFaceName -1); out s
Posts: 795
Threads: 136
Joined: Feb 2009
nice..
but the font is not exactly the same, seems more "polished" on native tooltip, like if AA was on or something like that...
|