需要帮助在C#中创建时间函数 [英] need help create time function in c#

查看:105
本文介绍了需要帮助在C#中创建时间函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


到目前为止,我正在尝试使用C#创建时间函数

Hi
i am trying to create time function in c# so far i have done this

class common
{

    public static int RandNumber()
    {
        Random randomNumber = new Random();
        int RN = randomNumber.Next(23,26);
        return RN;
    }

    public void TimeCheck(string handler)
    {
        Timer time = new Timer();
        time.Elapsed += new ElapsedEventHandler(handler);
        time.Interval = (1000) * (1);
        time.Enabled = true;
        time.Start();

    }


    void timer_Tick(object sender, EventArgs e)
    {
        label.Text = DateTime.Now.ToString();
    }
}



我正在尝试创建一个可以重复使用的通用函数,但是所有方法都会给我带来错误.



i am trying to create one common function which i can use again and again but it all ways give me error .

推荐答案

您要实现什么?由于您发布的代码无法配合使用-这三个部分是分开的,奇数的,并且似乎不起作用.

例如,随机数函数实际上是没有用的:
What are you trying to achieve? Because the code you have posted does not fit together - the three parts are separate, odd, and don''t appear to work.

For example, the random number function is really useless:
public static int RandNumber()
{
    Random randomNumber = new Random();
    int RN = randomNumber.Next(23,26);
    return RN;
}

如果您连续多次拨打此电话,几乎可以肯定会得到相同的号码.将您的随机数生成器移动到类级别变量:

If you call this several times in quick succession, you will almost certainly get the same number. Move you random number generator to a class level variable instead:

Random randomNumber = new Random();
public static int RandNumber()
   {
   return randomNumber.Next(23,26);
   }

几乎问到为什么把它作为一个单独的方法呢?"

另外,请不要在代码中使用幻数",而应将其转换为具有合理名称的const值.这样,如果需要更改它们,则只需要在一个地方进行更改即可.


您的其他方法也很奇怪:我认为您没有详细考虑您的任务.

Which almost asks "why have it as a separate method at all?"

Plus, don''t use "magic numbers" in you code - convert them to const values with sensible names instead. That way, if they need to change, you only have to change them in one place.


Your other method is equally odd: I don''t think you have thought about your task in any detail.


我看到您正在使用System.Timers命名空间中的计时器.您尚未编写Elapsed事件处理程序.您已经编写了System.Windows.Forms.Timer类随附的Tick事件处理程序.

我想您是.Net或C#的新手.因此,我建议读一两本书,然后继续.
I see you are using timer from System.Timers namespace. You have not written the Elapsed event handler yet. You have written Tick event handler which comes with System.Windows.Forms.Timer class.

I guess you are new to .Net or C#. So I would suggest reading a book or two and then proceed.


您的timer_Tick方法未在UI线程上调用,因此您无法设置标签的文本.试试这个:

your timer_Tick method is not invoked on the UI thread thus you cannot set the text of the label. try this:

void timer_Tick(object sender, EventArgs e)
{
    if (label.InvokeRequired)
        label.BeginInvoke((MethodInvoker)(() => label.Text = DateTime.Now.ToString()));
    else
        label.Text = DateTime.Now.ToString();
}



而且ElapsedEventHandler不需要字符串,因此您可能需要更改



also ElapsedEventHandler doesn''t take a string, so you might want to change

time.Elapsed += new ElapsedEventHandler(handler);




to

time.Elapsed += timer_Tick;


这篇关于需要帮助在C#中创建时间函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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