相关疑难解决方法(0)

Process.start:如何获得输出?

我想从我的Mono/.NET应用程序运行外部命令行程序.例如,我想运行mencoder.可能吗:

  1. 要获取命令行shell输出,并将其写在我的文本框中?
  2. 要获得数值以显示时间流逝的进度条?

.net c# mono process.start

281
推荐指数
6
解决办法
27万
查看次数

为什么我的进程的退出方法没有被调用?

我有以下代码,但为什么ProcessExited从未调用该方法?如果我不使用Windows shell(startInfo.UseShellExecute = false),它也是一样的.

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = path;
startInfo.Arguments = rawDataFileName;
startInfo.WorkingDirectory = Util.GetParentDirectory(path, 1);

try
{
     Process correctionProcess = Process.Start(startInfo);
     correctionProcess.Exited += new EventHandler(ProcessExited);                   

     correctionProcess.WaitForExit();

     status = true;
}
Run Code Online (Sandbox Code Playgroud)

.....

internal void ProcessExited(object sender, System.EventArgs e)
{
      //print out here
}
Run Code Online (Sandbox Code Playgroud)

.net c#

90
推荐指数
5
解决办法
3万
查看次数

在没有分散控制台窗口的情况下在C#中启动进程

我想出了如何启动一个过程.但我现在的问题是控制台窗口(在这种情况下是7z)弹出最前面阻挡我的视线并移除我的焦点打断我的句子或每隔几秒钟做一次.它非常烦人,我如何防止这种情况发生.我认为CreateNoWindow解决了这个问题,但它没有.

注意:有时控制台需要用户输入(替换文件或不替换).所以完全隐藏它可能是个问题.

这是我目前的代码.

void doSomething(...)
{
    myProcess.StartInfo.FileName = ...;
    myProcess.StartInfo.Arguments = ...;
    myProcess.StartInfo.CreateNoWindow = true;
    myProcess.Start();
    myProcess.WaitForExit();
}
Run Code Online (Sandbox Code Playgroud)

c# process

54
推荐指数
3
解决办法
4万
查看次数

StandardOutput.ReadToEnd()挂起

我有一个经常使用外部程序并读取其输出的程序.它使用你通常的进程重定向输出很好地工作,但是当我尝试读取它时,由于某种原因,一个特定的参数会挂起,没有错误消息 - 没有例外,当它到达该行时它只是'停止'.我当然使用集中式函数来调用和读取程序的输出,这是:

public string ADBShell(string adbInput)
{
    try
    {
        //Create Empty values
        string result = string.Empty;
        string error = string.Empty;
        string output = string.Empty;
        System.Diagnostics.ProcessStartInfo procStartInfo 
            = new System.Diagnostics.ProcessStartInfo(toolPath + "adb.exe");

        procStartInfo.Arguments = adbInput;
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.RedirectStandardError = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;
        procStartInfo.WorkingDirectory = toolPath;
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo = procStartInfo;
        proc.Start();
        // Get the output into a string
        proc.WaitForExit();
        result = proc.StandardOutput.ReadToEnd();
        error = proc.StandardError.ReadToEnd();  //Some ADB outputs use this
        if (result.Length …
Run Code Online (Sandbox Code Playgroud)

c# stream hang redirectstandardoutput

45
推荐指数
3
解决办法
6万
查看次数

process.WaitForExit()异步

我想等待一个进程完成,但process.WaitForExit()挂起我的GUI.是否有基于事件的方式,或者我是否需要生成一个线程来阻止直到退出,然后自己委托事件?

.net c# user-interface asynchronous winforms

43
推荐指数
7
解决办法
3万
查看次数

在C#中获取视频文件的缩略图

我想显示我网站上列出的视频的缩略图,我想从视频中获取单帧(从特定时间开始)并将其显示为缩略图.

我试过这个http://ramcrishna.blogspot.com/2008/09/playing-videos-like-youtube-and.html但是没有用.

这可能是使用.NET C#吗?

.net c#

32
推荐指数
4
解决办法
4万
查看次数

如何在PowerShell中异步捕获进程输出?

我想从我在Powershell脚本中启动的进程捕获stdout和stderr,并将其异步显示到控制台.我已经通过MSDN和其他博客找到了一些关于这样做的文档.

创建并运行下面的示例后,我似乎无法异步显示任何输出.所有输出仅在进程终止时显示.

$ps = new-object System.Diagnostics.Process
$ps.StartInfo.Filename = "cmd.exe"
$ps.StartInfo.UseShellExecute = $false
$ps.StartInfo.RedirectStandardOutput = $true
$ps.StartInfo.Arguments = "/c echo `"hi`" `& timeout 5"

$action = { Write-Host $EventArgs.Data  }
Register-ObjectEvent -InputObject $ps -EventName OutputDataReceived -Action $action | Out-Null

$ps.start() | Out-Null
$ps.BeginOutputReadLine()
$ps.WaitForExit()
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我希望在程序执行结束之前在命令行上看到"hi"的输出,因为应该触发OutputDataReceived事件.

我已经尝试过使用其他可执行文件 - java.exe,git.exe等.所有这些都具有相同的效果,所以我不得不认为有一些我不理解或错过的简单.还需要做什么才能异步读取stdout?

powershell events asynchronous

17
推荐指数
3
解决办法
1万
查看次数

以正确的顺序捕获进程stdout和stderr

我从C#启动一个进程如下:

public bool Execute()
{
    ProcessStartInfo startInfo = new ProcessStartInfo();

    startInfo.Arguments =  "the command";
    startInfo.FileName = "C:\\MyApp.exe";

    startInfo.UseShellExecute = false;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;

    Log.LogMessage("{0} {1}", startInfo.FileName, startInfo.Arguments);

    using (Process myProcess = Process.Start(startInfo))
    {
        StringBuilder output = new StringBuilder();
        myProcess.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)
        {
            Log.LogMessage(Thread.CurrentThread.ManagedThreadId.ToString() + e.Data);
        };
        myProcess.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e)
        {
            Log.LogError(Thread.CurrentThread.ManagedThreadId.ToString() +  " " + e.Data);            
        };

        myProcess.BeginErrorReadLine();
        myProcess.BeginOutputReadLine();

        myProcess.WaitForExit();

    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

但这有一个问题......如果有问题的应用程序按顺序写入std out和std err:

std out: msg 1 …
Run Code Online (Sandbox Code Playgroud)

c# multithreading stdout stderr

15
推荐指数
2
解决办法
6438
查看次数

进程有时在等待退出时挂起

我的进程在等待退出时挂起的原因可能是什么?

此代码必须启动 powershell 脚本,该脚本在内部执行许多操作,例如通过 MSBuild 开始重新编译代码,但问题可能是它生成了太多输出,并且即使在正确执行 power shell 脚本后,此代码在等待退出时也会卡住

这有点“奇怪”,因为有时这段代码运行良好,有时却卡住了。

代码挂在:

process.WaitForExit(ProcessTimeOutMiliseconds);

Powershell 脚本在 1-2 秒内执行,同时超时为 19 秒。

public static (bool Success, string Logs) ExecuteScript(string path, int ProcessTimeOutMiliseconds, params string[] args)
{
    StringBuilder output = new StringBuilder();
    StringBuilder error = new StringBuilder();

    using (var outputWaitHandle = new AutoResetEvent(false))
    using (var errorWaitHandle = new AutoResetEvent(false))
    {
        try
        {
            using (var process = new Process())
            {
                process.StartInfo = new ProcessStartInfo
                {
                    WindowStyle = ProcessWindowStyle.Hidden,
                    FileName = "powershell.exe",
                    RedirectStandardOutput = true,
                    RedirectStandardError = …
Run Code Online (Sandbox Code Playgroud)

c#

15
推荐指数
1
解决办法
2199
查看次数

Process.Start()从命令提示符窗口获取错误

我正在尝试使用args 启动命令promt进程.现在我想获取有关错误的信息(如果存在).

someProcess = System.Diagnostics.Process.Start(cmd, someArgs);
Run Code Online (Sandbox Code Playgroud)

最好的问候,洛维吉

c# process command-prompt

11
推荐指数
1
解决办法
1万
查看次数