i'm trying to use multithreading in my ASP.NET C# website to have a constant process that always checks for users who haven't logged for 30 days and delete them from my Database.
This is the code:
public static Semaphore MultiClean { get; set; }
Inside my Code-Behind PreInit function:
// Remove users who haven't logged in for more than 30 days
MultiClean = new Semaphore(1, 1);
Cleaner();
Cleaner function:
public static void Cleaner()
{
int i = 1;
while (true)
{
Thread thread = new Thread(new ParameterizedThreadStart(RemoveOld));
thread.Start(i);
i++;
}
}
RemoveOld function:
public static void RemoveOld(object args)
{
MultiClean.WaitOne();
List<User> UserLst = SQLDB.getInstance().returnAllUsers();
for (int i = 0; i < Convert.ToInt32(UserLst.Count); i++)
{
DateTime lastvisit = UserLst[i].lastvisit;
DateTime now = DateTime.Now;
TimeSpan diff = now - lastvisit;
if (diff.Days >= 30)
{
SQLDB.getInstance().DeleteUser(UserLst[i].id);
}
}
Thread.Sleep(30000);
MultiClean.Release(1);
}
After uploading it to my server and trying to access my website i get an error:
I followed the instructions in various websites including the first comment here: What is a semaphore?
No idea what am i doing wrong... Hope you could help me with the problem.. Thanks!