Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a Library Management System and I want to add a feature that when students add a book there will be a return period of 7 days. I have tried something but it's not running as a background. Is it necessary to work in the background? And I have put a notification system in the student window.

What I have tried:


What I have tried:

public class SchedulerService
{
    private static SchedulerService _instance;
    private List<Timer> timers = new List<Timer>();
        
    private SchedulerService() { }
        
    public static SchedulerService Instance => _instance ?? (_instance = new SchedulerService());
        
    public void ScheduleTask(int hour, int min, double intervalInHour, Action task)
    {
         DateTime now = DateTime.Now;
         DateTime firstRun = new DateTime(now.Year, now.Month, now.Day, hour, min, 0, 0);

         if (now > firstRun)
         {
             firstRun = firstRun.AddDays(1);
         }
        
         TimeSpan timeToGo = firstRun - now;

         if (timeToGo <= TimeSpan.Zero)
         {
             timeToGo = TimeSpan.Zero;
         }
        
         var timer = new Timer(x =>
                    {
                        task.Invoke();
                    }, null, timeToGo, TimeSpan.FromHours(intervalInHour));
        
         timers.Add(timer);
     }
}
    
public static class MyScheduler
{
    public static void IntervalInSeconds(int hour, int sec, double interval, Action task)
    {
        interval = interval / 3600;
        SchedulerService.Instance.ScheduleTask(hour, sec, interval, task);
    }

    public static void IntervalInMinutes(int hour, int min, double interval, Action task)
    {
        interval = interval / 60;
        SchedulerService.Instance.ScheduleTask(hour, min, interval, task);
    }

    public static void IntervalInHours(int hour, int min, double interval, Action task)
    {
        SchedulerService.Instance.ScheduleTask(hour, min, interval, task);
    }

    public static void IntervalInDays(int hour, int min, double interval, Action task)
    {
        interval = interval * 24;
        SchedulerService.Instance.ScheduleTask(hour, min, interval, task);
    }
}


Main window:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
            Timer.MyScheduler.IntervalInDays(12, 00, 7,
            () => {
                this.Dispatcher.Invoke(() =>
                {
                    NotificatonTxtBox.Text = "You need to return your book" + Environment.NewLine;
                });
            });
        }
Posted
Updated 19-May-21 10:05am

1 solution

Um.
You do realize that all of that code is part of your app, and the process it encompasses? Which means that it won't "wait seven days" unless the app is actually running for the whole time - as soon as the app closes the timers are destroyed. So it will never "work in the background"!

And if you add to that the load on the system once you get a reasonable number of students - how many are there at your school? - it's a bad idea.

Instead, do two things.
1) Store a "return date" in the DB when the student borrows the book. That's easy: DateTime.Now.Date.AddDays(7) will give you that.
2) When the student logs in, check if he has any loans out and if so, when the "return date" is compared to now. You can then let the student know in advance.

I'd also add a "librarian only" function which lets you query the DB for overdue books, and an alert when returning the book to fine the offender.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900