0

我正在编写一个启动测试器 (.cmd) 的应用程序,因此我正在传递已输入到列表框中的测试。如果输入了一个测试,此方法工作得很好,但如果有 2 个或更多,它会给我错误:

“System.dll 中发生‘System.ComponentModel.Win32Exception’类型的未处理异常附加信息:系统找不到指定的文件”

和两者在调试器中看起来都是正确的StartInfo.FilenamecurrentTestFromListbox[i]

有人知道我哪里出错了吗?

我很抱歉我的代码令人困惑——我只是一个初学者。

public void executeCommandFiles()
    {
        int i = 0;
        int ii = 0;
        int numberOfTests = listboxTestsToRun.Items.Count;

    executeNextTest:
        var CurrentTestFromListbox = listboxTestsToRun.Items.Cast<String>().ToArray();
        string filenameMinusCMD = "error reassigning path value";
        int fileExtPos = CurrentTestFromListbox[i].LastIndexOf(".");

        if (fileExtPos >= 0)
        {
            filenameMinusCMD = CurrentTestFromListbox[i].Substring(0, fileExtPos);
        }

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.Arguments = @"pushd Y:\Tests\" + filenameMinusCMD + @"\" + CurrentTestFromListbox[i];
        startInfo.WorkingDirectory = @"pushd Y:\Tests\" + filenameMinusCMD + @"\";
        startInfo.FileName = CurrentTestFromListbox[i];
        Process.Start(startInfo);

        //Wait for program to load before selecting main tab
        System.Threading.Thread.Sleep(10000);

        //Select MainMenu tab by sending a left arrow keypress
        SendKeys.Send("{LEFT}");

        i++;

        if (i < numberOfTests)
        {
            checkIfTestIsCurrentlyRunning:

            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName.Contains("nameOfProgramIAmTesting"))
                {
                    System.Threading.Thread.Sleep(2000);

                    //if (ii > 150)

                    if (ii > 6) //test purposes only
                    {
                        MessageBox.Show("The current test (" + filenameMinusCMD + ") timed out at 5 minutes. The next test has been started.", "Test timed out",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error,
                            MessageBoxDefaultButton.Button1);
                    }

                    ii++;

                    goto checkIfTestIsCurrentlyRunning;
                }
                goto executeNextTest;
            }
        }
    }
}

谢谢!-乔尔

4

1 回答 1

1

这是您重构的代码。睡眠/ gotos 真的很困扰我。我无法真正测试它,但我认为它应该可以正常工作。让我知道它是否不起作用或您有任何问题。

这假设您的列表框包含以下内容:

testname.cmd
test2.cmd
test3.exe
lasttest.bat

这是我的尝试:

public void executeCommandFiles()
{
    foreach (string test in listboxTestsToRun.Items)
    {
        //gets the directory name from given filename (filename without extension)
        //assumes that only the last '.' is for the extension. test.1.cmd => test.1
        string testName = test.Substring(0, test.LastIndexOf('.')); 

        //set up a FileInfo object so we can make sure the test exists gracefully.
        FileInfo testFile = new FileInfo(@"Y:\Tests\" + testName + "\\" + test);

        //check if it is a valid path
        if (testFile.Exists)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo(testFile.FullName);
            //get the Process object so we can wait for it to finish.
            Process currentTest = Process.Start(startInfo);
            //wait 5 minutes then timeout (5m * 60s * 1000ms)
            bool completed = currentTest.WaitForExit(300000);
            if (!completed)
                MessageBox.Show("test timed out");
            //use this if you want to wait for the test to complete (indefinitely)
            //currentTest.WaitForExit();
        }
        else
        {
            MessageBox.Show("Error: " + testFile.FullName + " was not found.");
        }
    }
}
于 2013-06-28T14:23:39.640 回答