1

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: Error message

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!

Community
  • 1
  • 1
argamanza
  • 1,122
  • 3
  • 14
  • 36
  • Why do this in site? Why don't to use the Windows Services for this? – VMAtm May 22 '15 at 12:44
  • @VMAtm The reason is it's a project which should be on ASP.NET and C# only, and available to open in any computer / web server without too much hassle... – argamanza May 22 '15 at 23:17
  • You are starting the cleaning thread for each user of your site. This will work very slowly. – VMAtm May 23 '15 at 07:16

0 Answers0