Показать сообщение отдельно
Старый 14.06.2011, 19:00   #5  
someOne is offline
someOne
Участник
Аватар для someOne
 
174 / 432 (15) +++++++
Регистрация: 11.12.2008
Адрес: Москва
Вот исправленный текст функции shellExecuteWaitInfo.

В прошлом варианте не работала настройка waitTime - время ожидания окончания процесса...

X++:
static client server str shellExecuteWaitInfo(str _commandLine, str _arguments = "", int _waitTime = -1)
{
    System.String                       outputInfo;
    System.IO.StreamReader              streamReader;
    System.Diagnostics.Process          process;
    System.Diagnostics.ProcessStartInfo startInfo;
    InteropPermission                   permission = new InteropPermission(InteropKind::ClrInterop);
    System.Exception                    exception;
    Boolean                             exited;

    System.Text.Encoding                encoding;
    System.Text.Encoding                encoding866;
    System.Text.Encoding                encodingUTF;

    System.Byte[]                       tmp;
    str                                 ret;
    ;
    permission.assert();

    encodingUTF = System.Text.Encoding::get_Unicode();
    encoding866 = System.Text.Encoding::GetEncoding(866);


    startInfo   = new System.Diagnostics.ProcessStartInfo(_commandLine);
    process     = new System.Diagnostics.Process();

    if (_arguments)
    {
        startInfo.set_Arguments(_arguments);
    }

    try
    {
        startInfo.set_UseShellExecute(false); // для отключения диалога безопасности доступа к сетевым файлам
        startInfo.set_RedirectStandardOutput(true);

        process.set_StartInfo(startInfo);
        process.Start();

        streamReader = process.get_StandardOutput();

        encoding = streamReader.get_CurrentEncoding();

        process.WaitForExit(_waitTime);

        exited = process.get_HasExited();

        if (exited == false)
        {
            process.Kill();

            return "";
        }

        outputInfo = streamReader.ReadToEnd();

        tmp = encoding.GetBytes(outputInfo);

        tmp = System.Text.Encoding::Convert(encoding866, encodingUTF, tmp);

        ret = encodingUTF.GetString(tmp);

        if (ret)
            return ret;

        return "ok";
    }
    catch (Exception::CLRError)
    {
        exception = CLRInterop::getLastException();

        while (exception)
        {
            error(exception.get_Message());

            exception = exception.get_InnerException();
        }

        return "";
    }
}
За это сообщение автора поблагодарили: Logger (5), Link (4).