进程exe卡在特定文件上以及如何在循环中移动下一个文件 [英] Process exe stuck at specific files and how to move next file in the loop

查看:82
本文介绍了进程exe卡在特定文件上以及如何在循环中移动下一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图在我的代码中运行一个循环遍历每个文件并执行一些任务。有时EXE在运行某些文件时会卡住。而这并没有向前发展。如果出现这种情况,我怎么能跳过过程中卡住
的文件,并将文件保存到失败的TXT文件中。并进一步移动下一个文件进行处理。我试着在网上检查它并没有得到太多帮助。请建议我怎样才能完成这项工作?

   foreach      var   file  < span class ="kwd"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit ; vertical-align:baseline; color:#101094"> in   files    
{
ProcessStartInfo processStartInfo = ProcessStartInfo
{
FileName = exedir + " \\testing.exe"
参数 = " -a -h -i -l" + 文件
RedirectStandardOutput = true
UseShellExecute =
CreateNoWindow = true
};

使用
Process exeProcess < span class ="pun"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit ; vertical-align:baseline; color:#303336"> = 过程 开始 processStartInfo ))
{
VAR <跨度类=" PLN "风格=" 保证金:0像素;填充:0像素;边框:0像素;字体风格:继承;字体变:继承;字体重量:继承;行高:继承; FONT-FAMILY:继承;垂直对齐:基线;颜色:#303336">行 = 字符串 ;

使用
StreamReader streamReader < span class ="pun"style ="margin:0px; padding:0px; border:0px; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; font-family:inherit ; vertical-align:baseline; color:#303336"> = exeProcess StandardOutput
{
(! streamReader EndOfStream
{
VAR <跨度类=" PLN "风格=" 保证金:0像素;填充:0像素;边框:0像素;字体风格:继承;字体变:继承;字体重量:继承;行高:继承; FONT-FAMILY:继承;垂直对齐:基线;颜色:#303336">内容 = streamReader ReadLine ();
//在这里做一些任务
}
}
exeProcess
WaitForExit 10000 );
}
}

解决方案

您正在调用WaitForExit超时。如果发生超时,则返回false。如果返回false,则将当前文件添加到文本文件中,然后继续。请注意,你的while循环实际上永远不会让WaitForExit
运行因为它会阻止等待进程完成。你应该切换到

async
读取。这将允许您处理输出但仍然调用WaitForExit来阻止。这将消除while循环。 


你需要考虑的另一件事是如果它超时会发生什么。如果你杀了这个过程你可能会破坏数据(取决于你的过程)。但你可能不想离开这个过程运行


I am trying to run a process in my code looping through each file and performing some task. Sometimes the EXE gets stuck when it runs across some files. And this doesn't move forward. If this kind of situation occurs how can I skip the files which is stuck in the process and save the file into a TXT file which has failed. And further move with the next file for processing. I tried to check it online and didn't get much help. Please suggest how can I get this done?

foreach (var file in files)
{
  ProcessStartInfo processStartInfo = new ProcessStartInfo
  {
      FileName = exedir + "\\testing.exe",
      Arguments = "-a -h -i -l " + file,
      RedirectStandardOutput = true,
      UseShellExecute = false,
      CreateNoWindow = true
  };

  using (Process exeProcess = Process.Start(processStartInfo ))
  {
      var line = string.Empty;

      using (StreamReader streamReader = exeProcess.StandardOutput)
      {
          while (!streamReader.EndOfStream)
          {
             var content = streamReader.ReadLine();
         // Doing some task here
          }
      }
      exeProcess.WaitForExit(10000);
  }
}

解决方案

You are calling WaitForExit with a timeout. If the timeout occurs it returns false. If it returns false then add the current file to your text file and then continue on. Note however that that your while loop is actually never going to let the WaitForExit to run because it'll block waiting for the process to finish. You should switch to async reading instead. This will allow you to process the output but still call WaitForExit to block. This will eliminate the while loop. 

The other thing you need to consider is what happens if it does time out. If you kill the process you may corrupt data (depends on your process). But you probably don't want to leave the process running.


这篇关于进程exe卡在特定文件上以及如何在循环中移动下一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆