当被事件淹没时,试图理解线程

时间:2013-05-06 13:01:11

标签: vb.net multithreading events

目前我正在编写一个使用我的kinect传感器的程序。

我非常擅长使用vb.net。

我已经阅读了许多关于线程的不同帖子,但它们通常涉及按钮点击事件。在一个事件中,当用户按下窗体上的按钮时,该按钮将启动一个新线程来执行一些例程,因此它不会冻结主UI。

然而,当涉及到Kinect传感器,例如Skeleton Stream时,它会不断更新每秒30帧。我觉得好像被事件淹没了。

当我尝试通过使用“Thread.Sleep”功能来减慢速度时,我的UI显然会锁定。我正在尝试启动一个新的线程来处理特定手势中的动作,但我不确定天气我可以使用我读过的一些线程示例或如何在事件触发30帧时在代码中实现它们第二

这是我在“SkeltonFrameReady”事件子程序中的代码,它是我想单独编程的“sendkeys”部分,并且主UI更新了人手的位置。

也许我需要在一个单独的线程上使用整个子程序?但我需要标签显示人手的相关位置实时更新,“Thread.Sleep”会阻止这种情况发生。

感谢您对处理此问题的想法。

Public Sub SensorSkeletonFrameReady(ByVal sender As Object, ByVal e As SkeletonFrameReadyEventArgs)

    Dim skeletons(-1) As Skeleton
    Dim passiveCount As Integer = 0
    Dim activeCount As Integer = 0

    Using skeletonFrame As SkeletonFrame = e.OpenSkeletonFrame()
        If skeletonFrame IsNot Nothing Then
            skeletons = New Skeleton(skeletonFrame.SkeletonArrayLength - 1) {}
            skeletonFrame.CopySkeletonDataTo(skeletons)

        End If
    End Using

    If skeletons.Length <> 0 Then
        For Each skel As Skeleton In skeletons

            '// Count the Number of People in the room up to six
            If (skel.TrackingState = SkeletonTrackingState.Tracked) Then
                activeCount = (activeCount + 1)
            ElseIf (skel.TrackingState = SkeletonTrackingState.PositionOnly) Then
                passiveCount = (passiveCount + 1)
            End If
            numPeople = (activeCount + passiveCount)
            Dim People As String = numPeople.ToString
            HowManyPeople.Text = "Number of People: " & People

            ''// Focus on the closest Person
            Dim closestID As Integer = 0
            If Not Me.kinect.SkeletonStream.AppChoosesSkeletons Then
                Me.kinect.SkeletonStream.AppChoosesSkeletons = True
                ' Ensure AppChoosesSkeletons is set
            End If
            ' Start with a far enough distance
            Dim closestDistance As Single = 10000.0!

            If (skel.Position.Z < closestDistance) Then
                closestID = skel.TrackingId
                closestDistance = skel.Position.Z
            End If

            If (closestID > 0) Then
                ' Track this skeleton

                Me.kinect.SkeletonStream.ChooseSkeletons(closestID)


                '//Swipe Right and Left
                If ((skel.Joints(JointType.HandRight).Position.Z < skel.Joints(JointType.ElbowRight).Position.Z) _
                    AndAlso (skel.Joints(JointType.HandLeft).Position.Y < skel.Joints(JointType.ElbowLeft).Position.Y)) Then
                    ' //right hand below head height 
                    If ((skel.Joints(JointType.HandRight).Position.Y < skel.Joints(JointType.Head).Position.Y) _
                        AndAlso (skel.Joints(JointType.HandRight).Position.Y > skel.Joints(JointType.ShoulderRight).Position.Y)) Then
                        ' //right hand right of left shoulder
                        If (skel.Joints(JointType.HandRight).Position.X > skel.Joints(JointType.ShoulderLeft).Position.X) Then

                            Dim XPosition = skel.Joints(JointType.HandRight).Position.X - skel.Joints(JointType.ShoulderRight).Position.X
                            CurrentHandPositionX.Text = "Horizontal-Position: " & XPosition.ToString
                            If XPosition > OldPosition Then
                                OldPosition = XPosition
                                KinectGesture.Text = "Swipe Right"


                                ''//I would like this to be on a separte thread
                                Dim WshShell = CreateObject("WScript.Shell")
                                WshShell.SendKeys("{Right}")
                                Thread.Sleep(220)

                            ElseIf XPosition < OldPosition Then
                                OldPosition = XPosition
                                KinectGesture.Text = "Swipe Left"

                                ''//I would like this to be on a separte thread as well
                                Dim WshShell = CreateObject("WScript.Shell")
                                WshShell.SendKeys("{Left}")
                                Thread.Sleep(220)

                            End If
                        End If
                    End If
                End If



                '//Selection "Press"
                If ((skel.Joints(JointType.HandRight).Position.Z < skel.Joints(JointType.ElbowRight).Position.Z) _
                       AndAlso (skel.Joints(JointType.HandLeft).Position.Y < skel.Joints(JointType.ElbowLeft).Position.Y)) Then
                    '//right hand below head height 
                    If ((skel.Joints(JointType.HandRight).Position.Y < skel.Joints(JointType.Head).Position.Y) _
                        AndAlso (skel.Joints(JointType.HandRight).Position.Y > skel.Joints(JointType.ShoulderRight).Position.Y)) Then
                        '//right hand right of left shoulder
                        If (skel.Joints(JointType.HandRight).Position.X > skel.Joints(JointType.ShoulderLeft).Position.X) Then

                            Dim ZPosition = skel.Joints(JointType.ShoulderCenter).Position.Z - skel.Joints(JointType.HandRight).Position.Z
                            CurrentHandPositionZ.Text = "Depth-Position: " & ZPosition.ToString
                        End If
                    End If
                End If




                '//Fast Scroll Right
                If ((skel.Joints(JointType.HandRight).Position.Z < skel.Joints(JointType.ElbowRight).Position.Z) _
                AndAlso (skel.Joints(JointType.HandLeft).Position.Y > skel.Joints(JointType.ShoulderLeft).Position.Y) _
                AndAlso (skel.Joints(JointType.HandLeft).Position.Y < skel.Joints(JointType.Head).Position.Y)) Then
                    ' //right hand below head height 
                    If ((skel.Joints(JointType.HandRight).Position.Y < skel.Joints(JointType.Head).Position.Y) _
                        AndAlso (skel.Joints(JointType.HandRight).Position.Y > skel.Joints(JointType.ShoulderRight).Position.Y)) Then
                        ' //right hand right of left shoulder
                        If (skel.Joints(JointType.HandRight).Position.X > skel.Joints(JointType.ShoulderRight).Position.X) Then
                            KinectGesture.Text = "Fast Scroll Right"
                            Dim WshShell = CreateObject("WScript.Shell")
                            WshShell.SendKeys("{Right}")
                        End If
                    End If
                End If

            End If
        Next

    End If

End Sub

1 个答案:

答案 0 :(得分:0)

“它是”sendkeys“部分我想单独编写”

而不是:

Dim WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys("{Right}")

您可以直接使用内置于VB.Net中的SendKeys.Send()

SendKeys.Send("{Right}")

对于线程部分,您的目标是通过在它们之间放置延迟来限制箭头键吗?如果是,您可以使用ToolBox中的标准Timer()来打开/关闭。您可以设置一个布尔值来指示是否仍然应该发送箭头,然后在Tick()事件中检查以确定何时关闭计时器。