如何在不使用计时器控件的情况下在指定的时间显示提醒 [英] How to show a reminder in specified time without using a Timer Control

查看:82
本文介绍了如何在不使用计时器控件的情况下在指定的时间显示提醒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai我想向用户显示提醒列表.提醒可以进行编辑.
我需要在不使用TIMER控件的情况下实现它.
通过C#代码帮助我.

Hai I want to show a list of reminders to users. The reminder can be edited.
I need to implement it without using a TIMER Control.
Help me through C# code.

推荐答案

使用线程(此代码未经测试,因此您可能需要对其进行调整).以下代码允许间隔或一天中的某个时间.

Use a thread (this code is untested, so you may have to tweak it). The following code allows for either an interval or a time of day.

using System.Threading;

public partial class MyForm
{
    Thread m_reminder = new Thread(new ParameterizedThreadStart(ReminderThread));

    public MyForm()
    {
        m_reminder.Start(new ThreadParams(new TimeSpan(0,1,0,0,0), true));
    }

    private void ReminderThread(ThreadParams params)
    {
        DateTime now = DateTime.Now;
        DateTime execTime;
        bool needNewTime = true;
        if (params.AsInterval)
        {
            execTime = now + params.Time;
        }
        else
        {
            execTime = new DateTime(now.Year, now.Month, now.Day, params.Time.Hours, params.Time.Minutes);
            if (execTime > now)
            {
                needNewTime = false; 
            }
        }
        try
        {
            while (true)
            {
                if (needNewTime)
                {
                    if (params.AsInterval)
                    {
                        execTime = execTime.Add(params.Time);
                    }
                    else
                    {
                        exectTime = execTime.AddDays(1);
                    }
                }
                if (DateTime.Now >= execTime)
                {
                    needNewTime = true;
                    // do something...
                }
                else
                {
                    Thread.Sleep(1000);
                }    
            }
        }
        catch (ThreadAbortException)
        {
            // eat the exception
        }
    }
}

public class ThreadParams
{
    public TimeSpan Time   { get; set; }
    public bool Asinterval { get; set; }
    public ThreadParams(TimeSpan time, bool asInterval)
    {
        Time = time;
        AsInterval = asinterval;
    }
}



编辑==============

修改以更正用于启动线程的单参数问题.



EDIT ==============

Modified to correct single-parameter issue for starting a thread.


使用系统托盘功能.其余 [ ^ ]的文章可能会对您有所帮助.
Use the System Tray feature.Remainder[^] article might help you.


这篇关于如何在不使用计时器控件的情况下在指定的时间显示提醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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