0

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

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

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

这是表格1

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

这是表格2

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

结束类

任何帮助将不胜感激。

4

1 回答 1

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
于 2013-11-13T08:25:15.613 回答