C# DateTime.Now在foreach循环中有一些副作用需要注意。当您处理项目时,您将与“随您移动”进行比较的时间,对于这样的快速循环可能没有多大关系,但如果这用于处理可能需要很长时间的循环,则取决于您想要的行为,您可能希望将其移动到循环之外的var n
C# DateTime.Now在foreach循环中有一些副作用需要注意。当您处理项目时,您将与“随您移动”进行比较的时间,对于这样的快速循环可能没有多大关系,但如果这用于处理可能需要很长时间的循环,则取决于您想要的行为,您可能希望将其移动到循环之外的var n,c#,list,function,C#,List,Function,DateTime.Now在foreach循环中有一些副作用需要注意。当您处理项目时,您将与“随您移动”进行比较的时间,对于这样的快速循环可能没有多大关系,但如果这用于处理可能需要很长时间的循环,则取决于您想要的行为,您可能希望将其移动到循环之外的var now=DateTime.now并使用now.Subtract。(…在循环中。(这更多是针对未来的读者,而不是答案的海报)请参阅关于使用DateTime时实现者应注意的事项。现在,在循环中使用。谢谢,这使我走上了正确的道路。我只是不知道如何才能做
DateTime.Now在foreach循环中有一些副作用需要注意。当您处理项目时,您将与“随您移动”进行比较的时间,对于这样的快速循环可能没有多大关系,但如果这用于处理可能需要很长时间的循环,则取决于您想要的行为,您可能希望将其移动到循环之外的
var now=DateTime.now
并使用now.Subtract。(…
在循环中。(这更多是针对未来的读者,而不是答案的海报)请参阅关于使用DateTime时实现者应注意的事项。现在,在循环中使用。谢谢,这使我走上了正确的道路。我只是不知道如何才能做到这一点。我已经删除了负号,因为我把逻辑搞混了。@ScottChamberlain,编辑。
class RestartData
{
public string ProgramLocation { get; set; }
public bool Active { get; set; }
public int RestarInterval { get; set; }
public bool RestartIfRunning { get; set; }
public string ProgramServer { get; set; }
public string ProcessName { get; set; }
public DateTime LastRestartTime { get; set; }
}
namespace ApplicationWatcher
{
public class RestartApplicationTask
{
public Int32 SleepInterval { get; set; }
public String Status { get; set; }
public String Error { get; set; }
public bool Stopping { get; set; }
public static log4net.ILog log { get; set; }
public void Start()
{
Stopping = false;
Error = "";
Status = "Starting";
if (SleepInterval == 0)
{
SleepInterval = 3600;
}
//make sure there is a logger
if (log == null)
{
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["log4net"])));
log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
//This calls a method within the class. The name of the class is not needed, but the list and name of list is needed.
List<RestartData> restartWorkList = AppRestartList();
try
{
processRestartList(restartWorkList);
}
catch (Exception e)
{
//Replace with logging
log.Info("Error: could not process " + restartWorkList);
}
}
public static Happy.Common.HappyTools.DB GetCurrentlyRunningApplications(bool Active)
{
String qry= "SELECT ProgramLocation, Active, RestartIfRunning FROM any_database.dbo.AppWatcher WITH (NOLOCK) WHERE Active = 1";
//qry = qry.Replace("<Active>", Active.ToString());
SqlCommand cmd = new SqlCommand(qry, (SqlConnection)DB.MakeConnection("any_database"));
DataTable dt;
Happy.Common.HappyTools.DB returnValue = new Happy.Common.HappyTools.DB();
try
{
dt = DB.ExecuteTable(cmd, "any_database");
DataRow thisRow = dt.Rows[0];
}
catch (Exception e)
{
//do something
}
finally { }
return returnValue;
}
static List<RestartData> AppRestartList()
{
List<RestartData> restartDatas = new List<RestartData>();
//Don't need to create a new instance but does need to be assigned to at least null
RestartData restartData = null;
//This is an actual SQL Query that would be the same as in a SQL manager.
string sqlQuery = "Select RestartTime, ProgramLocation, LastRestartTime, RestartInterval, ProgramServer, RestartIfRunning, ProcessName from dbo.AppWatcher where active=1;";
//execute sql query
//execute database reader
SqlCommand command = new SqlCommand(sqlQuery, (SqlConnection)DB.MakeConnection("any_database"));
DataTable dt;
command.Connection.Open();
dt = DB.ExecuteTable(command, "any_database");
foreach (DataRow row in dt.Rows)
{
//move stuff from reader into log data, and exp
//not RestartData restartData = new RestartData(); The RestartData() method is already declared
restartData = new RestartData();
restartData.ProgramLocation = Misc.NullSafeString(row["programLocation"]);
//restartData.Active = Misc.NullSafeBool(row["active"]);
restartData.RestarInterval = Misc.NullSafeInt(row["restartInterval"]);
restartData.RestartIfRunning = Misc.NullSafeBool(row["restartIfRunning"]);
restartData.ProcessName = Misc.NullSafeString(row["processName"]);
restartData.LastRestartTime = Misc.NullSafeDateTime(row["lastRestartTime"]);
//add restartData to list
restartDatas.Add(restartData);
}
return restartDatas;
}
static void processRestartList(List<RestartData>restartJob)
{
//i represents all the properties in LogData
**foreach(RestartData i in restartJob)
{
if (i <= RestartData())
{
i.LastRestartTime.AddHours(-1);
}
}**
}
}
}
foreach(var data in restartJob.Where(x=> DateTime.Now.Subtract(x.LastRestartTime).TotalSeconds > x.RestarInterval))
{
//kill
}
// Check what LastRestartTime is and if it is greater that RestartInterval, kill the process and restart it.
DateTime dt = DateTime.Now; // Keep DateTime static.
foreach(RestartData restartData in restartJob)
{
if (dt >= restartData.LastRestartTime.AddHours(restartData.RestarInterval))
{
restartData.LastRestartTime = dt;
// restart process
}
}