我有3个同一个应用运行的实例。如果我有我的程序标记要杀死它,它工作正常,2继续运行。当我标记另一个被杀死时,它会杀死剩下的2个,而不仅仅是一个。如果我在“break;”之前放置一个消息框,它可以正常工作,只会再杀一个并留下一个正在运行。但是如果没有消息框,当我尝试杀死第二个实例时,它总是会杀死一个和第三个实例。知道为什么一个消息框会让它正常工作吗?我假设某种类型的计时问题,但我无法弄清楚为什么,因为有一个“休息”。
foreach (WatcherScraperModel scraper in this.Scrapers.Except(scheduled.Where(x => x.Active)))
{
totalinstances = 0;
foreach (Process process in Process.GetProcesses())
{
try
{
if (process.MainModule.FileName == scraper.FileName)
{
// get instances already running
totalinstances++;
}
}
catch
{
// certain core processes cannot be accessed, don't worry about it
}
}
foreach (Process process in Process.GetProcesses())
{
try
{
// only kill an instance if there's too many running
if (process.MainModule.FileName == scraper.FileName && totalinstances > scheduled.Count(x => x.Active && x.FileName == scraper.FileName))
{
logger.DebugFormat("Killed {0} because it was not scheduled to be running", scraper.FileName);
process.Kill();
scraper.RestartRemaining = 0;
break;
}
}
catch
{
// certain core processes cannot be accessed, don't worry about it
}
}
}