form2上的按钮没有响应

时间:2013-11-13 04:59:19

标签: vb.net

我的第一个表单只有一个notifyicon并轮询一些结果,如果结果符合标准并且引发警报,则会打开form2。

我的问题是一旦form2打开结果并播放闹钟mp3,我想用来停止mp3播放的页面上的按钮根本没有响应。

编辑,我已将我的代码更改为我目前拥有的代码。当建议使用后台工作程序时,Form2变得无法响应。

这是form1

Public Class Form1
Public Property waittimer As Integer = 60000
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    BackgroundWorker1.WorkerSupportsCancellation = True
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick

End Sub

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

Private Sub wait(ByVal interval As Integer)
    Dim stopW As New Stopwatch
    stopW.Start()
    Do While stopW.ElapsedMilliseconds < interval
        ' Allows your UI to remain responsive
        Application.DoEvents()
    Loop
    stopW.Stop()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

    Dim firsttime As Boolean = True
    Dim myForm2 As Form2
    ' While True

    Try

        ' Create a request for the URL. 
        Dim request As WebRequest = _
          WebRequest.Create("http://example/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, "|")

        Form2.maxcalls = responseArray(0)
        Form2.cph = responseArray(1)
        Form2.mht = responseArray(2)
        Form2.alarm = responseArray(3)

        'Form2.Show()
        If Form2.alarm = True Then
            myForm2 = New Form2()
            Form2.Show()
        End If
        Application.DoEvents()
        System.Threading.Thread.Sleep(waittimer)
        ' wait(waittimer)

        If waittimer = 1800000 Then
            waittimer = 60000
        End If

    Catch ex As Exception

    End Try
    '  End While
End Sub
End Class

这是form2

Public Class Form2
Public Property maxcalls As Integer
Public Property cph As Integer
Public Property mht As Integer
Public Property alarm As Boolean
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    InitializeComponent()
    startsong()

End Sub


Sub startsong()
    MHTvalue.Text = mht.ToString
    Maxcallsvalue.Text = maxcalls
    CPHvalue.Text = cph
    Dim audio As New AudioFile("C:\example\Help.mp3")
    audio.Play()
    Application.DoEvents()
End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MessageBox.Show("test")
End Sub

结束班

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

尝试使用Form1。请注意底部有一个新的小助手类:

Imports System.Net
Imports System.IO
Public Class Form1

    Private WithEvents Tmr As New Timer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        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

    End Sub

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

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Try
            ' Create a request for the URL. 
            Dim request As WebRequest = _
              WebRequest.Create("http://example/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)
            e.Result = D
        Catch ex As Exception
            e.Result = Nothing
        End Try
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        If Not IsNothing(e.Result) Then
            Dim D As Data = DirectCast(e.Result, Data)
            If D.alarm Then
                Form2.startsong(D)
            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
End Class

Form2现在变为:

Public Class Form2

    Public Sub startsong(ByVal D As Data)
        Me.Show()
        MHTvalue.Text = D.mht.ToString
        Maxcallsvalue.Text = D.maxcalls
        CPHvalue.Text = D.cph
        Dim audio As New AudioFile("C:\example\Help.mp3")
        audio.Play()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MessageBox.Show("test")
    End Sub

End Class