抢答器服务端,其中抢答接受功能涉及到了线程编程,因为单线程的话,如果程序处于等待监听数据状态,那整个程序就会处于停滞无法继续其他操作。所以New一个新的线程来专门管理数据接收将会解决这个问题。
下面是抢答开始按钮进行多线程创立的源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim myUdpDataReceive As UdpDataReceive Dim intPort As Integer Dim b As Integer = 1 Dim ListenThread As Thread intPort = Integer.Parse(TextBox2.Text) myUdpDataReceive = New UdpDataReceive(intPort) ListenThread = New Thread(AddressOf myUdpDataReceive.AcceptData) ListenThread.Start() If True Then TextBox1.Text += "第" & i & "次抢答成功开始!" + vbCrLf i = i + 1 End If End Sub |
数据接收源码UDP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
Public Sub AcceptData() Dim i As Integer = 1 Try Dim strdataAccept As String = ""< Dim myEndPoint As New IPEndPoint(IPAddress.Parse("127.0.0.1"), intPort) ' Do myUdpClient = New UdpClient(intPort) Dim myByte As Byte() = myUdpClient.Receive(myEndPoint) strdataAccept = Encoding.Unicode.GetString(myByte) ReceiveMessage = strdataAccept If ReceiveMessage.Length > 0 And Not ReceiveMessage = "chenzhiguo" Then Dim Soundpath As String = "system.wav" playSound(Soundpath) Dialog1.Label2.Text = ReceiveMessage + ",抢答成功!" 'Form11.ShowDialog() Dialog1.ShowDialog() 'MessageBox.Show(ReceiveMessage + "恭喜您,抢答成功!", "抢答成功") i = i + 1 Else Dim Soundpath As String = "error.wav" playSound(Soundpath) Dialog1.Label1.Text = "错误:" Dialog1.Label2.Text = "集体抢答失败请重来!" Dialog1.ShowDialog() 'MessageBox.Show("OH!My God!本次接受出错,请进行下次抢答!", "接收出错", MessageBoxButtons.OK, MessageBoxIcon.Error) End If 'Loop myUdpClient.Close() Catch ex As Exception ErrMessage = ex.Message End Try End Sub |
至于其他的源码在附件中的整个项目文件中将会整体包含。开发环境VS2010+VB.Net+Access。如需源码,请留言,或者发送邮件至:admin@uugu.org