Hi,@BgnerNprg207. Welcome to Microsoft Q&A.
Solution:
1.Use the timer to determine whether it has timed out in real time.
2.Use SQL query to determine whether it has timed out
3.When it times out, use NotifyIcon to notify
Reference Code:
public partial class Form1 : Form
{
private Timer timer;
private NotifyIcon notification1;
public Form1()
{
InitializeComponent();
//Set the timer
timer = new Timer()
{
Enabled = true,
Interval = 3000, //How often to execute in milliseconds
};
timer.Tick += _timer_Tick;
//Setting up Notifier
notification1 = new NotifyIcon();
notification1.Visible = true;
notification1.Icon = SystemIcons.Warning; // The Icon property sets the icon that will appear in the systray for this application.
}
private void _timer_Tick(object sender, EventArgs e)
{
using (MySqlConnection conn = new MySqlConnection(" ")) //Write your database connection string
{
conn.Open();
MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM db_vehicle_maintenance_schedule WHERE schedule >= DATE_SUB(NOW(), INTERVAL 1 DAY)", conn);
MySqlDataReader reader = cmd1.ExecuteReader();
if (reader.HasRows)
{
notification1.ShowBalloonTip(50000, "Attention!", "A vehicle schedule for \nmaintenance is detected. \nPlease check your car in your garage.", ToolTipIcon.Warning);
}
}
}
}
Attach:
1.If you can tell me in more detail how schedule and INTERVAL 1 DAY are designed, I will be able to more accurately determine whether the following statement will work correctly
MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM db_vehicle_maintenance_schedule WHERE schedule >= DATE_SUB(NOW(), INTERVAL 1 DAY)", conn);
2.NotifyIcon reference documentation: NotifyIcon Class (System.Windows.Forms) | Microsoft Learn
Optimization suggestions:
In this process, the timer executes SQL multiple times, and the SQL query consumes too much performance.
Is it possible to execute SQL only once to query schedule and INTERVAL 1 DAY, and combine DateTime date = DateTime.Now in the application to determine whether it has timed out?
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.