請讓我知道如何解決以下錯誤。內存溢出僅在生產計算機中部署代碼後纔會發生(即一旦Windows服務在生產計算機上啓動並運行)。它不是在我們當地的複製品。.Net Windows服務內存異常
Application: LSRAnalysisService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.OutOfMemoryException
Stack:
at System.TimeZoneInfo.GetDaylightTime(Int32, AdjustmentRule)
at System.TimeZoneInfo.GetIsDaylightSavingsFromUtc(System.DateTime, Int32, System.TimeSpan, AdjustmentRule, Boolean ByRef)
at System.TimeZoneInfo.GetUtcOffsetFromUtc(System.DateTime, System.TimeZoneInfo, Boolean ByRef, Boolean ByRef)
at System.DateTime.ToLocalTime(Boolean)
at System.DateTime.FromFileTime(Int64)
at System.Timers.ElapsedEventArgs..ctor(Int32, Int32)
at System.Timers.Timer.MyTimerCallback(System.Object)
at System.Threading.TimerQueueTimer.CallCallbackInContext(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.TimerQueueTimer.CallCallback()
at System.Threading.TimerQueueTimer.Fire()
at System.Threading.TimerQueue.FireQueuedTimerCompletion(System.Object)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
我們有一個定時器在windows服務中運行。
請提前發現你的幫助下
protected override void OnStart(string[] args)
{
if (wcfserviceHost != null)
wcfserviceHost.Close();
wcfserviceHost = new ServiceHost(typeof(AnalysisManager));
wcfserviceHost.Open();
Deletefile();
}
protected void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Deletefile();
}
private void Deletefile()
{
try
{
timer = new System.Timers.Timer();
timer.Interval = (1000) * 60 * 60;//(10000) * 6;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
string FilePath = System.Configuration.ConfigurationSettings.AppSettings["FilePath"];
DirectoryInfo dirInfo = new DirectoryInfo(FilePath);
foreach (DirectoryInfo subDirectory in dirInfo.GetDirectories())
{
if (subDirectory.Name == "Temp") continue;
if (subDirectory.CreationTime < DateTime.Now.AddDays(-1))
{
subDirectory.Delete(true);
}
}
timer.Enabled = true;
timer.Start();
}
catch(Exception ex)
{
}
}
}
}
感謝代碼。
爲什麼不使用'System.Threading.Timer'?另外,'System.Timers.Timer'是一次性的,你必須處置它。 – Dennis