Joshua Dela Cruz
Joshua Dela Cruz

Reputation: 235

C# Winforms: Background Notification for every users when the date specified in the Database has come

I am new with C#, so and currently working with a windows app that has a timer working in background that comes from the SQLServer database.

For example:

A user entered the system, then checks the payment terms and then one client appeared to pay the monthly bill. The paid for the bill for December 1, 2016 up-to December 31, 2016. And then the user will process it and scheduled the next payment for January 1, 2017. And when January 1, 2017 comes there will be a notification bubble or forms to notify the user that the client has to pay this day.

So far, I have no codes yet because I am just in the part of brainstorming and gathering information about the project.

The question is, How can I notify a user in an specific date that is based on the specified date from the Database?

Thank you!

Upvotes: 0

Views: 638

Answers (1)

Sebi
Sebi

Reputation: 3979

If the user only should be informed, when your application is active, this easily could be done via polling for example. You use a timer which ticks every second or sth. you want. In the Timer.Elapsed event you can poll the date from your Database and check it's value.

So make a class:

public class CheckDbDate
{
   private Timer _timer;
   public event EventHandler DateReached;

   public CheckDbDate()
   {
      _timer = new Timer();
      _timer.Intervall = 500; //ms
      _timer.Elapsed += (sender, e) => CheckDate();
      _timer.Start();
   }

   public void CheckDate()
   {
      //Check your Database Date value
      if (DateTime.Now.CompareTo(dbDate) >= 0)
      {
         DateReached?.Invoke(this, new EventArgs());
      }
   }
}

So in your MainApplication Code you have to instatiate a CheckDbDate object and subscribe for the DateReached Event. You maybe need to make this stuff async, because it will block your UI every 500ms.

This is the easiest approach i think.

If they should be informed without having your application running. You should think about a WindowsService (WCF).

Upvotes: 1

Related Questions