为什么我从C#调用时编译的AutoIt脚本无法返回?

时间:2017-01-26 20:06:05

标签: c# autoit

我编译的AutoIt脚本本身运行正常但是从C#调用时它会运行但是没有完成执行。我编译的C#代码:

Process myExe = new Process();
Environment.CurrentDirectory = Path.GetDirectoryName(exeDir); //exeDir is full path of the AutoIt executable directory
ProcessStartInfo StartInfo = new ProcessStartInfo("");
StartInfo.FileName = exeFile; //exeFile is full path of the executable itself
StartInfo.Verb = "runas";
myExe = Process.Start(StartInfo);

它在AutoIt代码中暂停EnvUpdate(),最后再在其他一些函数上暂停。如果我手动运行可执行文件,则不会发生这种情况。

我从C#启动了一个运行此可执行文件的批处理文件。以及CreateNoWindowUseShellExecute的组合。也没有成功: 1)以管理员身份运行C#编译的可执行文件。 2)使用StartInfo.CreateNoWindowStartInfo.UseShellExecute的组合。 3)从批处理文件运行AutoIt可执行文件。 4)通过批处理文件从Perl文件运行AutoIt可执行文件。 5)从Windows计划任务运行AutoIt可执行文件。 6)使用以下任一项运行不带ProcessStartInfo的AutoIt可执行文件:

myExe = Process.Start("cmd.exe", "/c start /wait " + exeFile);

myExe = Process.Start(exeFile);

1 个答案:

答案 0 :(得分:0)

下面是一个示例C#类,用于使用.WaitForExit()外部调用已编译的AutoIt脚本(EXE文件),这应该是您的问题:

using System.Configuration;
using System.Diagnostics;

namespace RegressionTesting.Common.ThirdParty
{
    public class ClickAtBrowserPosition
    {
        public static void CallClickAtBrowserPosition(string currentBrowserWindowTitle, int xPosition, int yPosition)
        {
            var pathClickAtBrowserPosition = ConfigurationManager.AppSettings["pathClickAtBrowserPosition"];
            var clickAtBrowserPositionExe = ConfigurationManager.AppSettings["clickAtBrowserPositionExe"];

            var process = new Process();
            var startInfo = new ProcessStartInfo
            {
                WindowStyle = ProcessWindowStyle.Hidden, // don't show the cmd.exe which calls the program
                FileName = "cmd.exe",
                Arguments = @" /C cd " +
                            $@"""{pathClickAtBrowserPosition}"" && {clickAtBrowserPositionExe} ""{currentBrowserWindowTitle}"" {xPosition} {yPosition}"
            };

            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit(); // wait to finish the execution
        }
    }
}

已编译的AutoIt脚本用于单击窗口(在本例中为浏览器)的相对位置。