定时器运行一次,不连续

时间:2016-12-26 15:47:59

标签: c# timer

我为使用语言翻译道歉

我想要运行一次计时器。下一个博客。我将它保存到数据库中。

正在进行的通知......一次只需4秒

timer1:启用,间隔:4000

mycode的:

        public bool InternetKontrol()
    {
        try
        {
            System.Net.Sockets.TcpClient kontrol_client = new System.Net.Sockets.TcpClient("www.google.com.tr", 80);
            return true;
        }
        catch (Exception)
        {

            label1.Text = "";
            return false;
        }                      


    }


private void timer1_Tick(object sender, EventArgs e)
    {


        if (InternetKontrol()==false)
        {


            NetGelenGiden ngg = new NetGelenGiden();
            ngg.GidenZaman = "Net Gitti " + DateTime.Now.ToLongTimeString().ToString();
            ngg.GelenZaman = "";


            dat.NetGelenGidens.Add(ngg);
            dat.SaveChanges();



            notifyIcon1.Icon = SystemIcons.Information;
            notifyIcon1.ShowBalloonTip(3000, "Net Durum", "Net Gitti " + DateTime.Now.ToLongTimeString().ToString(),ToolTipIcon.Info);
            notifyIcon1.Visible = true;



            label2.Text ="Net Gitti "+ DateTime.Now.ToLongTimeString().ToString();




        }

1 个答案:

答案 0 :(得分:1)

我认为您在Windows窗体中定义了Timer控件(因为您没有放置timer1声明)。

在事件timer1_Tick开始时,禁用或停止计时器:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        timer1.Start();
    }

    private bool InternetKontrol() { /* your code */ }

    public bool flagInternetService = true;

    private void timer1_Tick(object sender, EventArgs e)
    {
        var newResult = InternetKontrol();
        var warn = false;
        if (flagInternetService != newResult){
            if (newResult == false) warn = true;
            flagInternetService = newResult;
        }
        if (warn)
        {
            /* your code warning */
            label2.Text = DateTime.Now.ToLongTimeString();
        }
    }
}