简体   繁体   English

我应该如何使我的方法按时运行

[英]c# How should I make my method run on time ticker

This is just a simple problem that I wanted to solve for the sake of improving my skills. 这只是一个简单的问题,我想通过提高自己的技能来解决。 I want my method selectingtime to run and I also want the condition I put in the RunEvent to receive the value of string selecttime from selectingtime. 我希望我的方法selectingtime运行,并且我还希望我放入RunEvent中的条件从selectingtime接收字符串selecttime的值。 If you did reply on this, I thank you so much. 如果您回答了这个问题,我非常感谢。

public partial class Form1 : Form
{
    int hourt;  
    int minutet;
    string hourformatt;
    string selectedtime;
    Timer tm = new Timer(); 

    public Form1()
    {
        InitializeComponent();           
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        tm.Interval = 1000; 
        tm.Tick += new EventHandler(RunEvent); 
        tm.Start();  
    }

    private void RunEvent(object sender, System.EventArgs e)
    {
        label1.Text = DateTime.Now.ToLongTimeString(); 
        DateTime dateT = DateTime.Now; // created datetime

           if (dateT.ToString("hh:mm tt") == selectedtime) // condition where dateT.ToString is equal to selectedtime 
           {
               MessageBox.Show("Please work"); // expected output whenever dateT.ToString is equal to selected time.
           }                
    }

    private void selectingtime()
    {
        hourt = Convert.ToInt32(textBox1.Text); //textbox for hour
        minutet = Convert.ToInt32(textBox2.Text); // textbox for minute
        hourformatt = textBox3.Text; // textbox for AM or PM
        selectedtime = hourt + ":" + minutet + " " + hourformatt; // example 05:31 PM
    }

    private void button1_Click(object sender, EventArgs e)  
    {
        selectingtime();         
    }
private void RunEvent(object sender, System.EventArgs e)
{
    selectingtime(); //This?
    label1.Text = DateTime.Now.ToLongTimeString(); 

    DateTime dateT = DateTime.Now; // created datetime

   if (dateT.ToString("hh:mm tt") == selectedtime) // condition where dateT.ToString is equal to selectedtime 
   {
       MessageBox.Show("Please work"); // expected output whenever dateT.ToString is equal to selected time.
   }
}

Though, I suppose you may get an error on the INT32 converting when the texbox is null, empty, or has non-numerical characters. 不过,我想当texbox为null,为空或具有非数字字符时,您可能会在INT32转换上遇到错误。 In that case, you may want to implement a try parse. 在这种情况下,您可能需要实现try解析。 Also, if you are stuck on the idea of "Getting" the select time from the method, then you need to have it return a string -- though this may meet your request, I don't think it is necessarily an "improvement". 另外,如果您坚持使用“从方法中获取选择时间”的想法,则需要让它返回一个字符串-尽管这可能满足您的要求,但我认为这不一定是“改进” 。

PS You do not need to need to create a tick event with a new event handler instance, passed with the event method. PS您不需要通过事件方法传递的新事件处理程序实例来创建滴答事件。

    private void Form1_Load(object sender, EventArgs e)
    {
        tm.Interval = 1000;
        tm.Tick += RunEvent;
        tm.Start();
    }

    private void RunEvent(object sender, System.EventArgs e)
    {
        var selectedTime = selectingtime();
        label1.Text = DateTime.Now.ToLongTimeString();

        DateTime dateT = DateTime.Now; // created datetime

        if (dateT.ToString("hh:mm tt") == selectedTime) // condition where dateT.ToString is equal to selectedtime 
        {
            textBox1.Clear();
            textBox2.Clear();
            MessageBox.Show("Please work"); // expected output whenever dateT.ToString is equal to selected time.
        }
    }

    private string selectingtime()
    {
        DateTime time;
        string timeFormat = string.Format("{0}:{1} {2}", textBox1.Text, textBox2.Text, textBox3.Text);
        DateTime.TryParse(timeFormat, out time);
        return time.ToString("hh:mm tt");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM