当处理大容量系统时,由于c#window服务而变得缓慢
本文关键字:服务 缓慢 c#window 由于 大容量 处理 系统 | 更新日期: 2023-09-27 18:20:32
下面我粘贴了一个c#代码,我在windows服务中使用线程方法,在服务初始时,在系统上的一些进程变得缓慢后,它正常工作,有时我会收到错误,比如系统超时内存执行之类的错误。
有人能告诉我我的线程方法是否好吗?建议
我正在使用计时器事件,它将每三分钟启动一次-->(DueTime)
(CsvGenFromDatabase)-->这是我在启动方法时调用的方法,它将调用这个中的一些子方法
protected override void OnStart(string[] args)
{
strNowDate = DateTime.Now.ToLongTimeString();
timerjob.Elapsed += new ElapsedEventHandler(CsvGenFromDatabase);
timerjob.Interval = Convert.ToDouble(DueTime);
timerjob.Enabled = true;
eventLog1.WriteEntry("my service started");
}
protected override void OnStop()
{
strNowDate = DateTime.Now.ToLongTimeString();
eventLog1.WriteEntry("my service stopped");
}
private void CsvGenFromDatabase(object sender, EventArgs e)
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew)) // Transaction Scope Started
{
Thread threadITD = new Thread(new ThreadStart(FileGenerationForITD)); // Thread Initialize for ITD
Thread threadCTD = new Thread(new ThreadStart(FileGenerationForCTD)); // Thread Initialize for CTD
Thread threadCID = new Thread(new ThreadStart(FileGenerationForCID)); // Thread Initialize for CID
Thread threadFFM = new Thread(new ThreadStart(FileGenerationForFFM)); // Thread Initialize for FFM
try
{
if ((threadITD == null) || (threadITD.ThreadState == System.Threading.ThreadState.Stopped) || (threadITD.ThreadState == System.Threading.ThreadState.Unstarted))
{
threadITD.Start(); // Thread Started for ITD
//LogError(1, "Form1", "CsvGenFromDatabase", "ITD Thread Started", "ITD Thread Started");
}
if ((threadCTD == null) || (threadCTD.ThreadState == System.Threading.ThreadState.Stopped) || (threadCTD.ThreadState == System.Threading.ThreadState.Unstarted))
{
threadCTD.Start(); // Thread Started for CTD
// LogError(1, "Form1", "CsvGenFromDatabase", "CTD Thread Started", "CTD Thread Started");
}
if ((threadCID == null) || (threadCID.ThreadState == System.Threading.ThreadState.Stopped) || (threadCID.ThreadState == System.Threading.ThreadState.Unstarted))
{
threadCID.Start(); // Thread Started for CID
// LogError(1, "Form1", "CsvGenFromDatabase", "CID Thread Started", "CID Thread Started");
}
if ((threadFFM == null) || (threadFFM.ThreadState == System.Threading.ThreadState.Stopped) || (threadFFM.ThreadState == System.Threading.ThreadState.Unstarted))
{
threadFFM.Start(); // Thread Started for FFM
//LogError(1, "Form1", "CsvGenFromDatabase", "FFM Thread Started", "FFM Thread Started");
}
}
catch (Exception ex)
{
objErrorLog.SrtErrorText = ex.ToString().Substring(0, 25);
objErrorLog.StrErrorDescription = ex.ToString();
objErrorLog.WriteErrorLog(objErrorLog);
}
finally
{
scope.Complete();
}
}
}
线程是否正忙于等待?
换言之,在等待发生的循环中循环,并通过这种搅动来燃烧循环?一个人应该把逻辑放在无事可做时避免如此繁忙的等待中。要做到这一点,只需做
线程.睡眠(0);
其通知CLR将线程的时间片放弃给可能实际需要它的另一个线程,并且该线程将在其下一周期开始时以最快的方式再次启动。