2013-11-21 34 views
0

我有一個檢查每60秒去,如果警報是真的,那將打開窗口2播放歌曲等試圖改變一次按鈕計時器按下

但Form2上的按鈕應設置waittimer = 180000這應該使下一次檢查發生半小時後。

我不習慣使用計時器,我做錯了什麼?

Public Class Form1 
Public Property waittimer As Integer 
Public Property playsong As Boolean = True 
Private WithEvents Tmr As New Timer 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    If (Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1) Then 
     Application.Exit() 
    End If 
    Tmr.Enabled = False 
    Tmr.Interval = TimeSpan.FromSeconds(60).TotalMilliseconds 
    BackgroundWorker1.RunWorkerAsync() 
End Sub 

Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick 
    playsong = False 
    Try 


     ' Create a request for the URL. 
     Dim request As WebRequest = _ 
      WebRequest.Create("http://example.com/example.aspx") 
     ' If required by the server, set the credentials. 
     request.Credentials = CredentialCache.DefaultCredentials 
     ' Get the response. 
     Dim response As WebResponse = request.GetResponse() 
     ' Display the status. 
     Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) 
     ' Get the stream containing content returned by the server. 
     Dim dataStream As Stream = response.GetResponseStream() 
     ' Open the stream using a StreamReader for easy access. 
     Dim reader As New StreamReader(dataStream) 
     ' Read the content. 
     Dim responseFromServer As String = reader.ReadToEnd() 
     ' Display the content. 
     Console.WriteLine(responseFromServer) 
     ' Clean up the streams and the response. 
     reader.Close() 
     response.Close() 

     Dim responseArray() As String 
     responseArray = Split(responseFromServer, "|") 

     Dim D As New Data 
     D.maxcalls = responseArray(0) 
     D.cph = responseArray(1) 
     D.mht = responseArray(2) 
     D.alarm = responseArray(3) 
     Form2.startsong(D) 
    Catch ex As Exception 

    End Try 
    playsong = True 
End Sub 

Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click 
    Application.Exit() 
    End 
End Sub 

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 
    Try 
     If waittimer = 180000 Then 
      Tmr.Interval = waittimer 
      Tmr.Start() 
      waittimer = 60000 
     Else 
      waittimer = 60000 
     End If 

     ' Create a request for the URL. 
     Dim request As WebRequest = _ 
      WebRequest.Create("http://example.com/example.aspx") 
     ' If required by the server, set the credentials. 
     request.Credentials = CredentialCache.DefaultCredentials 
     ' Get the response. 
     Dim response As WebResponse = request.GetResponse() 
     ' Display the status. 
     Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) 
     ' Get the stream containing content returned by the server. 
     Dim dataStream As Stream = response.GetResponseStream() 
     ' Open the stream using a StreamReader for easy access. 
     Dim reader As New StreamReader(dataStream) 
     ' Read the content. 
     Dim responseFromServer As String = reader.ReadToEnd() 
     ' Display the content. 
     Console.WriteLine(responseFromServer) 
     ' Clean up the streams and the response. 
     reader.Close() 
     response.Close() 

     Dim responseArray() As String 
     responseArray = Split(responseFromServer, "|") 

     Dim D As New Data 
     D.maxcalls = responseArray(0) 
     D.cph = responseArray(1) 
     D.mht = responseArray(2) 
     D.alarm = responseArray(3) 
     D.forcealarm = False 
     e.Result = D 
    Catch ex As Exception 
     Dim D As New Data 
     D.maxcalls = 404 
     D.cph = 404 
     D.mht = 404 
     D.alarm = True 
     D.forcealarm = True 
     e.Result = D 
    End Try 

    Tmr.Interval = waittimer 
End Sub 

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted 
    If Not IsNothing(e.Result) Then 
     Dim strtime As String 
     Dim englandtime = System.TimeZoneInfo.ConvertTime(Now, TimeZoneInfo.FindSystemTimeZoneById("UTC")) 
     strtime = DatePart(Microsoft.VisualBasic.DateInterval.Hour, englandtime) & DatePart(Microsoft.VisualBasic.DateInterval.Minute, englandtime) 

     Dim D As Data = DirectCast(e.Result, Data) 
     If D.forcealarm = True Then 
      Form2.startsong(D) 
     End If 
     If D.alarm Then 
      If strtime < 730 And strtime > 0 Then 

      Else 
       Form2.startsong(D) 
      End If 
     End If 
    End If 
    Tmr.Start() 
End Sub 

Private Sub Tmr_Tick(sender As Object, e As System.EventArgs) Handles Tmr.Tick 
    Tmr.Stop() 
    BackgroundWorker1.RunWorkerAsync() 
End Sub 

End Class 

Public Class Data 
Public Property maxcalls As Integer 
Public Property cph As Integer 
Public Property mht As Integer 
Public Property alarm As Boolean 
Public Property forcealarm As Boolean 
End Class 

窗體2

Public Class Form2 
Public audio As New AudioFile(My.Application.Info.DirectoryPath & "\Help.mp3") 
Public Sub startsong(ByVal D As Data) 
    Me.Show() 
    MHTvalue.Text = D.mht.ToString 
    Maxcallsvalue.Text = D.maxcalls 
    CPHvalue.Text = D.cph 
    If Form1.playsong = True Then 
     audio.Play() 
    End If 

End Sub 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    ' Me.Close() 
    audio.Stop() 
    Form1.waittimer = 180000 
End Sub 

Private Sub Form2_Closing(sender As Object, e As EventArgs) Handles MyBase.Load 
    audio.Stop() 
    Form1.waittimer = 180000 
End Sub 

Private Sub Form2_Minimize(sender As Object, e As EventArgs) Handles MyBase.Load 
    audio.Stop() 
    Form1.waittimer = 180000 
End Sub 
+0

這是什麼「不」做?從快速瀏覽你的代碼,看起來你正在嘗試在form1上設置計時器,而在form1中使用form1,而不是原始form1實例的實例? –

+0

我試圖改變計時器,用於從60秒到半個小時的結果。 form1只是坐在通知托盤 – Imsopov

+0

我沒有看到form2中的代碼實際上獲取form1的運行實例嗎?簡單引用form1就像引用form1的空白副本,而不是form1已經運行的實例。 –

回答

0

那可能是因爲你的原始Form1的實例計時器永遠不會再由你的代碼配置。而是在未運行(顯示)的表單1的新副本上設置計時器。

您需要獲取最初打開的form1的運行實例。我使用的一種技術是用於標記我將要查找的表單,然後遍歷當前正在運行的表單實例,通過它的標記獲取我的表單,將找到的表單設置爲與我正在處理的對象相同,然後使用該表單的屬性/方法。

將你的form1的標籤設置爲「myTag」。

然後在表格2:

 For Each f As Form In My.Application.OpenForms 

      If Not f.InvokeRequired Then 
       'Can access the form directly. 

       If f.Tag = "myTag" Then 
        'this is my form 
        Dim fcast As New form1 
        fcast = f 
        fcast.<---- use your methods/properties... 
       End If 

      End If 

     Next 
0

只要改變:

Public Property waittimer As Integer 

要:

Public Property waittimer As Integer 
    Get 
     Return Tmr.Interval 
    End Get 
    Set(value As Integer) 
     If value > 0 Then 
      Tmr.Interval = value 
     End If 
    End Set 
End Property 

你被改變waittimer值,但不是實際的時間間隔()的定時器也是如此。這將兩者聯繫在一起。