如何防止表单冻结(无法接收传入数据) - VB.NET问 [英] How to prevent form freezing (cannot receive incoming data) - VB.NET ask

查看:65
本文介绍了如何防止表单冻结(无法接收传入数据) - VB.NET问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am developing a two-player Quiz bee game LAN-Based. The players takes turn one at a time. A player will click a button to pick a question. On the first two turns, sending and receiving of data are working but on the third turn the other player didn't receive the incoming data.

Anyone can help me to avoid this problem?





我的尝试:





What I have tried:

**Variables for `UDPClient`**

    Public Module Server
        Public publisher As New Sockets.UdpClient(0)
        Public subscriber As New Sockets.UdpClient(54545)
    End Module

**Here is my code for TheGame.vb (Receiving data)**

    Private Sub tmrUDP_Tick(sender As Object, e As EventArgs) Handles tmrUDP.Tick
        Try
            Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
            Dim receiveBytes() As Byte = subscriber.Receive(ep)
            lblReceive.Text = ASCII.GetString(receiveBytes)

            If lblReceive.Text = "b1" Then
                bttn1.BackgroundImage = My.Resources.Bee_Button_Blue
                btn1 = False
                bluebtn1 = True
                UncheckRB()
                bttn1.Enabled = False
                bttn1.Visible = False
                pcbxb1.Visible = True
                ShowHideColorPanel()
                enableButtons()
                btnused1 = True

                publisher.Close()
                subscriber.Close()
                '=====================
            ElseIf lblReceive.Text = "b2" Then
                bttn2.BackgroundImage = My.Resources.Bee_Button_Blue

                btn2 = False
                bluebtn2 = True
                UncheckRB()
                bttn2.Enabled = False
                bttn2.Visible = False
                pcbxb2.Visible = True
                ShowHideColorPanel()
                enableButtons()
                btnused2 = True
                '=====================

    'same code until "b25"

    ElseIf lblReceive.Text = "r1" Then
                bttn1.BackgroundImage = My.Resources.Bee_Button_RED

                btn1 = False
                redbtn1 = True
                UncheckRB()
                bttn1.Enabled = False
                bttn1.Visible = False
                pcbxr1.Visible = True
                ShowHideColorPanel()
                enableButtons()
                btnused1 = True
                '=====================
            ElseIf lblReceive.Text = "r2" Then
                bttn2.BackgroundImage = My.Resources.Bee_Button_RED

                btn2 = False
                redbtn2 = True
                UncheckRB()
                bttn2.Enabled = False
                bttn2.Visible = False
                pcbxr2.Visible = True
                ShowHideColorPanel()
                enableButtons()
                btnused2 = True
                '=====================
    'same code until r25
    End Sub

**TheGame_Load**

    Private Sub TheGame_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    subscriber.Client.ReceiveTimeout = 100
        subscriber.Client.Blocking = False
    End Sub

**Here is the Sending Data event**(*button event from another `Form` to submit answer*)

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    If btn1 = True And TheGame.lblColor.Text = "Blue" Then

                            publisher.Connect(TheGame.lblIPother.Text, TheGame.lblPort.Text)
                            Dim sendByte() As Byte = ASCII.GetBytes("b1")
                            publisher.Send(sendByte, sendByte.Length)

                            AddScore()
                            TheGame.bttn1.BackgroundImage = My.Resources.Bee_Button_Blue
                            Me.Hide()
                            btn1 = False
                            bluebtn1 = True
                            UncheckRB()
                            TheGame.bttn1.Enabled = False
                            btnused1 = True
                            disableButtons()
                            ShowHideColorPanel()

    ElseIf btn1 = True And TheGame.lblColor.Text = "Red" Then

                            publisher.Connect(TheGame.lblIPother.Text, TheGame.lblPort.Text)
                            Dim sendByte() As Byte = ASCII.GetBytes("r1")
                            publisher.Send(sendByte, sendByte.Length)
                            AddScore()
                            TheGame.bttn1.BackgroundImage = My.Resources.Bee_Button_RED
                            Me.Hide()

                            btn1 = False
                            redbtn1 = True
                            UncheckRB()
                            TheGame.bttn1.Enabled = False
                            btnused1 = True
                            disableButtons()
                            ShowHideColorPanel()
    'same code until `btn25`
    End Sub

推荐答案

出现锁定的应用程序 up up通常表示在阻止UI消息管道的UI线程上长时间运行的任务处于活动状态。如果你确实使用Sam的代码,那么UDP任务就在sperate线程上。



要调试此项,当有一个长暂停/应用程序显示已锁定事件时,点击VS暂停按钮,看看应用程序何时忙碌。如果你没有登陆代码,请继续踩到代码。您将看到问题的确切位置。
Apps that appear "locked up" is usually an indication that a long running task is active on the UI thread blocking the UI message pipe. If you are indeed using Sam's code, then the UDP tasks are on a sperate thread.

To Debug this, when there is a long pause/"app appears locked" event, hit the VS pause button and see when the app is busy. If you don't land on code keep stepping until you do. You will see exactly where one of your problem(s) are.


这篇关于如何防止表单冻结(无法接收传入数据) - VB.NET问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆