C#中KinectV2手势触发异步打开窗体崩溃问题求助
手势触发打开窗体崩溃问题(KinectV2 + Project Praga)
我使用KinectV2与Project Praga实现手势识别,应用于媒体控制、智能家居控制等场景。当前遇到的问题是:通过手势触发异步方法打开MusicForm窗体时,该窗体发生崩溃;但通过按钮触发打开该窗体则可正常运行。
我已尝试将OnLoad方法修改为异步实现,并在Executor方法中添加线程ID检测、创建窗体句柄及通过BeginInvoke调用窗体显示的代码,获取到当前线程ID为6,但问题仍未解决。
初始崩溃代码
using Microsoft.Gestures; using Microsoft.Gestures.Endpoint; using System; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace UIControl { public partial class KinectProject : Form { private static GesturesServiceEndpoint _gesturesService; private static Gesture _One; private void Form1_Load(object sender, EventArgs e) { this.Text = "Kinect Project Test"; } public KinectProject() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); InitializeGestures(); } private async task InitializeGestures() { var gesturesServiceHostName = "localhost"; await RegisterGestures(gesturesServiceHostName); } private static async Task RegisterGestures(string gesturesServiceHostName) { _gesturesService = GesturesServiceEndpointFactory.Create(gesturesServiceHostName); await _gesturesService.ConnectAsync(); await RegisterOneGesture(); } private static async Task RegisterOneGesture() { var Iddle = new HandPose("iddle", new FingerPose(new AllFingersContext(), FingerFlexion.OpenStretched), new PalmPose(new AnyHandContext(), PoseDirection.Backward)); var hold = new HandPose("Hold", new FingerPose(Finger.Index, FingerFlexion.Open, PoseDirection.Up), new FingerPose(new[] { Finger.Thumb, Finger.Middle, Finger.Ring, Finger.Pinky }, FingerFlexion.Folded), new PalmPose(new AnyHandContext(), PoseDirection.Backward)); _One = new Gesture("One", Iddle, hold); _One.Triggered += (s, e) => Executor(s); await _gesturesService.RegisterGesture(_One, isGlobal: true); } private static void OnGestureDetected(object sender, GestureSegmentTriggeredEventArgs args, ConsoleColor foregroundColor) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("Gesture detected! : "); Console.ForegroundColor = foregroundColor; Console.WriteLine(args.GestureSegment.Name); Console.ResetColor(); } static void Executor(object sender, GestureSegmentTriggeredEventArgs args, ConsoleColor foregroundColor) { MusicForm V1 = new MusicForm(); V1.Show(); } } }
修改后的尝试代码
protected override async void OnLoad(EventArgs e) { base.OnLoad(e); await InitializeGestures(); } private void Executor(object sender) { int managedThreadId = Thread.CurrentThread.ManagedThreadId; MessageBox.Show("ManagedThreadId = " + managedThreadId); if (this.Handle == IntPtr.Zero) { CreateHandle(); } this.BeginInvoke((MethodInvoker)(delegate { MusicForm V1 = new MusicForm(); V1.Show(); })); }
内容的提问来源于stack exchange,提问作者DJxRott
相关产品推荐
相关产品推荐

