如何在C#中按时提醒事件? [英] how can I remind events on time in C#?

查看:75
本文介绍了如何在C#中按时提醒事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试但没有用,任何人都可以帮我解决这个问题吗?

我已经给用户一个设施来选择提醒类型(例如每日,每周[用户将被要求选择一天],在特殊日子)

当用户在填写这些字段(事件名称,事件描述,提醒类型,日期和时间)后添加事件时,应用程序应该提醒他事件在特定的时间。

请帮我完成这项任务?



我的代码 - 一种将被调用到主窗体的方法



I am trying but no use, Can anyone help me solve this?
I have given a facility to user to select the reminder type (such as daily, weekly[user will be asked to select a day], on a special day)
and when the user add an event after filling such fields (event name, event description, reminder type, date and time) and the app should remind him the events on specific time.
Please help me to do with this task?

My code - A method that will be called into the main form

public void Reminders()
        {
            SqlCeConnection conn = new SqlCeConnection("Data Source=E:\\Project Final_Year\\Project Final\\MyProject\\ERU Implementation\\VirtualPetedit\\VirtualPet\\MyDB.sdf");
            string query = "SELECT time FROM Events";
            SqlCeDataReader r;
            DateTime thisDay = DateTime.Today;
            

            
            try
            {
                conn.Open();
                SqlCeCommand command = new SqlCeCommand(query, conn);
                r  = command.ExecuteReader();
                //if (r.HasRows)
                       // {
                DateTime tag = r.GetDateTime(0);
                            while (r.Read())
                            {
                                int result = DateTime.Compare(thisDay, tag);
                                if (result == 0)
                                    MessageBox.Show("You have a reminder at this moment");

                                /*
                                DateTime tag = r.GetDateTime(0);
                                if (r.GetDateTime(0)  == thisDay )
                                {
                                    
                                    VirtualPet.Popup popup = new VirtualPet.Popup();
                                    popup.Reminder();
                                }*/
                            }
                       // }
                       /* else
                        {
                            MessageBox.Show("No Reminder times currently");                          
                        }*/
                conn.Close(); 
            }
            catch (Exception ex)
            { 
              //  MessageBox.Show(ex.Message.ToString());
            }
        }

推荐答案

这种方法的问题是提醒时间必须准确:到微秒或比较将失败。

而不是寻找相等,在数据库中添加一列,表示已发出并确认了提醒。然后将SQL SELECT更改为仅返回未使用的提醒。

并在循环中移动SqlReader的使用!

然后,将您的比较代码更改为:

The problem with that approach is that the reminder time has to be accurate: to the microsecond or the comparison will fail.
Instead of looking for equality, add a column to your DB which indicates that a reminder has been issued and acknowledged. Then change your SQL SELECT to only return "unused" reminders.
And move the usage of the SqlReader inside the loop!
Then, change your comparison code to:
try
    {
    conn.Open();
    using (SqlCeCommand command = new SqlCeCommand(query, conn))
        {
        r = command.ExecuteReader();
        DateTime now = DateTime.Now;
        while (r.Read())
            {
            DateTime tag = r.GetDateTime(0);
            if (tag <= now)
                }
                MessageBox.Show("You have a reminder at this moment");
                }
            }
        }
    conn.Close(); 
    }
catch (Exception ex)
    { 
    MessageBox.Show(ex.Message.ToString());
    }


这篇关于如何在C#中按时提醒事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆