2016-12-26 91 views
1

我很抱歉使用語言翻譯定時器運行一次,不連續

我想運行一次計時器。下一位博主。我將它保存到數據庫中。

在4秒內

持續notification..One

定時器1:啓用,時間間隔: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(); 




     } 
+0

你在哪裏定義Timer對象?我沒看到一個。 – Max

+0

怎麼這樣...定時器? – ynsbldk

+0

我想工作一次 – ynsbldk

回答

1

我認爲你是在Windows窗體定義定時器控制(這是因爲你不要把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(); 
     } 
    } 
} 
+0

你能否提供詳細信息..謝謝 – ynsbldk

+0

我編輯我的帖子 – Tito

+0

停止後它不起作用。給我一個警告,然後再繼續。我收到一次警告。 – ynsbldk