I am developing a .Net 4.0 C# Windows Service based (Dropbox like ) application which runs continuously and keeps a watch on a specific folder for content and updates and notify the changes onto the Database and similar clients application will be installed on other machines as well which will perform suitable updates to the same folder in their hosting machines. I have developed the whole application with multiple threads and recursion calls which will keep on performing and notifying the changes through-out. The application runs well for 3 hrs after 3 hrs it throws in a stack-overflow exception and crashes.
To be more specific I have 9 threads and some of them do the following jobs
1) threadFSWToDB – write any file changes(add,rename,delete) to DB.
2) threadDBToUploadLogList – creates a folder on the server where the other clients will download file from.
3) threadDoUpload – will upload file on the web server.
4) threadDBToDownloadList – will upload a list of changes to be performed by polling DB.
my question is, is this the feasible solution or do I need to re-consider my design for the whole app. If yes what would be the best way to perform the above tasks (sequential or parallel). And what is the most basic approach for performing the tasks which are never ending and continuous in nature like this one.
My Code is as below, I don’t know how much to put in so that it could make the matter clear
Thread threadFSWToDB = new Thread(() => WriteFSWLogListToDatabase());
private static void WriteFSWLogListToDatabase()
{
try
{
using (var objEntities = new ShareBoxEntities()) // EF class instantiation
{
// this DB class constructor call later on throws stack overflow exception not necessarily from this method
var objConnector = new DBConnector(objEntities);
var objConnector = new DBConnector();
List<string> directories = objConnector.GetAllSharedDirectoryRelativePaths();
if (FSWLogList != null && FSWLogList.Count() > 0)
{
for (int i = 0; i <= FSWLogList.Count - 1; i++)
{
// some file changes being written to Database
FSWLogList[i].SavedInDB = true;
}
}
}
}
}
catch (Exception ex)
{
// write to event log and mail to administrator
}
finally
{
if (FSWLogList != null && FSWLogList.Count > 0)
{
FSWLogList.RemoveAll(item => item.SavedInDB == true);
}
Thread.Sleep(5000);
WriteFSWLogListToDatabase();
}
}
if recursion is the main reason then why the application runs fine for some times a day or two and then starts exhibiting the above mentioned behaviour
Thanks everyone for their valuable time.
I have found out the solution to my problem, the recursive function calls have been replaced with a while(true) //loop
still being called on the respective threads
This ensure that my function which monitors the folder doesn’t complete its execution while a try and catch ensured it goes on even after an exception. The problem that I faced while using the while(true) was any resource used in a function was to be made available for the other functions running on separate thread, that was handled by calling the thread.sleep for a specific period. It may not be a universal solution to a similar problem but it fitted my requirement, so I would like others to just watch out before implementing the stated solution in a similar scenario.