hi here am using single threading in c#.net, let me explain you how it is working now if suppose job A is running in a long process until unless it job A was completed it will not go for job B, but here requirement is all the jobs should gets activated but none of the jobs get intrupted so how can i modify this threading, can any give some suggestions please asap
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();
}
This appears to already be multi-threaded. Try adding logging code to the start and end of each
FileGenerationForXXXmethod so you can see the four methods starting together and stopping separately.Additionally, you can knock out all of the
ifstatements. The thread objects are guaranteed to be in that state because nothing changed betweennewandStart().EDIT: In response to comments.
To prevent the timer from triggering a second time before the threads all complete, I suggest joining the threads before starting the timer again.
Thread.Join()causes this thread to sleep until the referenced thread has ended. All other threads contiunue uninterrupted.