【发布时间】:2011-07-18 10:09:18
【问题描述】:
我目前有一个大问题,下面的代码检查文件中的关键字“mp4:production/CATCHUP/”,如果找到它会启动一个可执行文件,尽管因为它找到了多个(完全相同)实例“mp4:production/CATCHUP/”它启动了几个进程。有没有限制这一点,以便在找到一个实例时它可能会停止查找?
我的代码如下:
string s = "";
private void CheckLog()
{
bool _found;
while (true)
{
_found = false;
Thread.Sleep(5000);
if (!System.IO.File.Exists("Command.bat")) continue;
using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat"))
{
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("test"))
{
_found = true;
break;
}
}
}
if (_found)
{
// Deletes filename in the log file, as the filename is instead handled by p.start
var result = Regex.Replace(s, @"test", string.Empty);
s = result;
RemoveEXELog(); // Deletes a specific keyword from Command.bat
RemoveHostFile();
Process p = new Process();
p.StartInfo.WorkingDirectory = "dump";
p.StartInfo.FileName = "test.exe";
p.StartInfo.Arguments = s;
p.Start();
p.WaitForExit();
MessageBox.Show("Operation Successful!");
string myPath = @"dump";
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = myPath;
prc.Start();
ClearLog(); // Deletes Command.bat and then creates a new empty Command.bat
LogTrue();
}
}
}
【问题讨论】:
-
在 (!found) {} 时制作。这太琐碎了,在发布之前至少花 10 分钟思考您的代码。
-
你为什么每天都开一个新账户?
-
@Henk Holterman 我使用 VPN,所以我不能使用 OPenID,或者它不记得我了
-
我一定是错过了什么,Hans Passant 的评论有什么问题?
-
@Jodrell 是什么让你觉得有?