kinect - How to run while(true) loop without freezing Windows form in C#? -
i writing application should show tracked skeletons 4 kinects. tracking , drawing on form should start when user presses start
button. coming java background know little using delegates
, eventhandlers
in c#. far code looks this, freezes form completely. how fix that?
private void mainwindow_load(object sender, eventargs e) { programloop(); } private void programloop() { int sensorcount = 0; foreach (kinectsensor connectedsensor in kinectsensor.kinectsensors) { if (connectedsensor.status == kinectstatus.connected) { connectedsensor.skeletonstream.enable(); connectedsensorsarray.add(connectedsensor); connectedsensor.start(); sensorcount++; } } } private void start_click(object sender, eventargs e) { trackskeletons(); } private void trackskeletons() { while (true) { foreach (kinectsensor sensor in connectedsensorsarray) { skeletonframe skeletonframe = sensor.skeletonstream.opennextframe(200); if (skeletonframe != null) { if (initskeletons == null) { initskeletons = new skeleton[skeletonframe.skeletonarraylength]; } skeletonframe.copyskeletondatato(initskeletons); skeletonframe.dispose(); skeleton initskeleton = initskeletons.where(s => s.trackingstate == skeletontrackingstate.tracked).firstordefault(); if (initskeleton != null) { switch (connectedsensorsarray.indexof(sensor)) { case 0: mainqueue.enqueue(initskeleton); break; case 1: secondqueue.enqueue(initskeleton); break; case 2: thirdqueue.enqueue(initskeleton); break; case 3: fourthqueue.enqueue(initskeleton); break; } } } } //dequeue skeletons , draw them } }
you should use timer instead of while(true)
.
your timer executed every x sec , when called, can call own function trackskeletons()
.
Comments
Post a Comment