This appears to already be multi-threaded. Try adding logging code to the start and end of each FileGenerationForXXX
method so you can see the four methods starting together and stopping separately.
private void FileGenerationForITD()
{
eventlog1.WriteEntry("FileGenerationForITD started.");
...
eventlog1.WriteEntry("FileGenerationForITD finished.");
}
Additionally, you can knock out all of the if
statements. The thread objects are guaranteed to be in that state because nothing changed between new
and Start()
.
Thread threadITD = new Thread(new ThreadStart(FileGenerationForITD));
Thread threadCTD = new Thread(new ThreadStart(FileGenerationForCTD));
// ...
try
{
ThreadITD.Start();
ThreadCTD.Start();
// ...
}
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.
private void CsvGenFromDatabase(object sender, EventArgs e)
{
timerjob.stop();
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
Thread threadITD = new Thread(new ThreadStart(FileGenerationForITD));
Thread threadCTD = new Thread(new ThreadStart(FileGenerationForCTD));
Thread threadCID = new Thread(new ThreadStart(FileGenerationForCID));
Thread threadFFM = new Thread(new ThreadStart(FileGenerationForFFM));
threadITD.Start();
threadCTD.Start();
threadCID.Start();
threadFFM.Start();
threadITD.Join();
threadCTD.Join();
threadCID.Join();
threadFFM.Join();
scope.Complete();
}
timerjob.start();
}
4
solved how to modify this single thread into multithreading in c# .net [closed]