我目前有一个大问题,下面的代码检查文件中的关键字“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();
}
}
}
答案 0 :(得分:2)
对于这种情况,我会使用Singleton类来管理工作流程。 Singleton将以全局线程安全的方式管理与_found
变量等效的内容。然后,所有线程都将查询此属性。
如下所示:
public sealed class Singleton
{
private static volatile Singleton instance;
private static object syncRoot = new Object();
private Singleton() {}
public bool Found { get; set; }
public static Singleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}
然后您的代码将如下所示:
private void CheckLog()
{
//bool _found; //not needed anymore
while (!Singleton.Instance.Found)
{
//_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("mp4:production/CATCHUP/"))
{
Singleton.Instance.Found = true;
break;
}
}
}
if (Singleton.Instance.Found)
{
// Deletes filename in the log file, as the filename is instead handled by p.start
var result = Regex.Replace(s, @"rtmpdump", 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();
}
}
}
答案 1 :(得分:1)
正如汉斯帕斯特所建议的那样,一旦发现它接近,明显的停止循环有什么问题?不需要单身人士。
private void CheckLog()
{
bool found = false;
while (!found)
{
//your code ...
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("test"))
{
_found = true;
break;
}
}
if (found)
{
//some more of your code ...
}
else
{
//get ready for the next iteration
}
}
}