【发布时间】:2012-01-16 04:55:29
【问题描述】:
您好,我在 c#.net 中使用单线程,如果假设作业 A 在一个很长的过程中运行,直到作业 A 完成,否则我将向您解释它现在是如何工作的,除非作业 A 完成它不会去作业 B,但是在这里要求是所有的工作都应该被激活,但没有一个工作被打断,所以我该如何修改这个线程,任何人都可以尽快给出一些建议
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)
{
timerjob.stop();
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
}
if ((threadCTD == null) ||
(threadCTD.ThreadState == System.Threading.ThreadState.Stopped) ||
(threadCTD.ThreadState == System.Threading.ThreadState.Unstarted))
{
threadCTD.Start(); // Thread Started for CTD
}
if ((threadCID == null) ||
(threadCID.ThreadState == System.Threading.ThreadState.Stopped) ||
(threadCID.ThreadState == System.Threading.ThreadState.Unstarted))
{
threadCID.Start(); // Thread Started for CID
}
if ((threadFFM == null) ||
(threadFFM.ThreadState == System.Threading.ThreadState.Stopped) ||
(threadFFM.ThreadState == System.Threading.ThreadState.Unstarted))
{
threadFFM.Start(); // Thread Started for FFM
}
}
catch (Exception ex)
{
objErrorLog.SrtErrorText = ex.ToString().Substring(0, 25);
objErrorLog.StrErrorDescription = ex.ToString();
objErrorLog.WriteErrorLog(objErrorLog);
}
finally
{
scope.Complete();
}
}
timerjob.start();
}
【问题讨论】:
-
@MitchWheat - 只能这么说......
-
@M.Babcock 请帮我把它改成多线程,我是这个窗口服务的新手
-
@pravz - 虽然这只是您发布的代码的许多明显问题之一,但让我向您介绍BackgroundWorker。
-
你……似乎不明白你的代码在做什么。
标签: c# windows-services