Scr1pter, I would respectfully echo the request for an example, as I'm running into
exactly the same trouble with C# macros in G1ANT. If AutoHotkey.Interop had a call for ahkReady(), I think this would be easier in general.
HOWEVER: You might try this (just added 'ahkReset();' as the last call):
Code: Select all
using AutoHotkey.Interop;
public class AutoHotKeyTest
{
public Object button; // Zugriff auf einen Button
public string nummer; // Button kann eigene Nummer ausführen
public void Ahk_Test()
{
var ahk = AutoHotkeyEngine.Instance; // Grab a copy of the AutoHotkey singleton instance
ahk.LoadFile("functions.ahk"); // Load a library or exec scripts in a file
ahk.ExecFunction("MyFunction", nummer); // Execute a specific function (found in functions.ahk), with 1 parameter
ahk.Reset();
}
}
I don't think that 'ahk.Reset()' command is documented on the AutoHotkey.Interop page, but I did find it in the source code. One problem may be solved, but another may crop up. Consider this code, which is not using AutoHotkey.Interop, but AutoHotkey.dll via its COM interface:
Code: Select all
ahkdll := ComObjCreate("AutoHotkey.Script")
ahkdll.ahktextdll("msgbox, Hello")
While ahkdll.ahkReady()
Sleep, 100
What happens SOMETIMES with C# (and I bet always if you use the COM interface) is that your code will need this ahkReady() call to keep your script from terminating before AHK gets a chance to run.
I'm probably rambling, but take a look at this code that works either
with or
without the ahk.Reset(). It's scripted and not compiled, taken from a G1ANT script, you'll see sort of what's happening with dlls and namespaces, but that's not really important here. Everything between '⊂⊃' is C#. If you put the delimiters on separate lines it's like an AH continuation section, I suppose).
Code: Select all
addon core version 4.100.19170.929
addon language version 4.100.19170.929
-dialog ♥macrodlls
♥macrodlls = System.dll,System.Drawing.dll,System.Windows.Forms.dll,AutoHotkey.Interop.dll
-dialog ♥macrodlls
♥macronamespaces = System,AutoHotkey.Interop,System.Windows.Forms
♥hellogiant = "Hello "
⊂
var ahk = AutoHotkeyEngine.Instance;
ahk.ExecRaw("MsgBox, Hello World!");
ahk.SetVar("x", ♥hellogiant);
ahk.SetVar("y", "World, from AHK and G1ANT!");
ahk.ExecRaw("z:=x . y");
string zValue = ahk.GetVar("z");
System.Windows.Forms.MessageBox.Show(zValue);;
ahk.SetVar("z", zValue);
ahk.ExecRaw("Msgbox, I can say %z%");
ahk.Reset();
⊃
It's more or less one of the examples from the AutoHotkey.Interop github page. But then look at this:
Code: Select all
addon core version 4.100.19170.929
addon language version 4.100.19170.929
-dialog ♥macrodlls
♥macrodlls = System.dll,System.Drawing.dll,System.Windows.Forms.dll,AutoHotkey.Interop.dll,System.Runtime.InteropServices.dll,System.Reflection.dll,Microsoft.CSharp.dll,System.Threading.Thread.dll
-dialog ♥macrodlls
♥macronamespaces = System,AutoHotkey.Interop,System.Windows.Forms,System.Reflection,System.Threading
-text.read C:\G1ANT\ie_com3.ahk result ♥ahkIn
♥ahkIn = ‴C:\G1ANT\ie_com4.ahk‴
⊂
var ahk = AutoHotkeyEngine.Instance;
//Load a library or exec scripts in a file
ahk.LoadFile(♥ahkIn);
MessageBox.Show("Stop thread");
ahk.Reset();
⊃
Without the System.Windows.Forms.MessageBox() call, which stops the thread, the whole C# script bails before the AHK runs. Take it out and see.
@HotKeyIt, could you please suggest a way to provide the same functionality as
Code: Select all
While ahkdll.ahkReady()
Sleep, 100
but using just AutoHotkey.Interop?
(I'm going to try to add a wrapped call to ahkReady() in a recompiled AutoHotkey.Interop, but frankly, I don't like my chances.)
A million thanks in advance,
burque505