Проблема с голосовым управлением C#

У меня проблема с голосовым управлением. Всё работает, но только один раз. Залез в интернет за поиском проблемы, рекомендуют запускать функцию прослушивания команд в цикле. Но я не знаю как это сделать... Прошу у вас помощи и совета. Пожалуйста, помогите. Вот весь код программы.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Kinect;
using System.Media;
using Microsoft.Speech.Recognition;
using Coding4Fun.Kinect.WinForm;


namespace WindowsFormsApp1
 {
public partial class Form1 : Form
{
    KinectSensor mysensor;
    SoundPlayer sp;
    


    public Form1()
    {
        InitializeComponent();
        
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Voice();

    }
    private void Voice()
    {
        sp = new SoundPlayer();
        sp.Stream = Properties.Resources.hello;
        sp.Play();
        System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("ru-ru");
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine(ci);
        sre.SetInputToDefaultAudioDevice();
        sre.SpeechRecognized += Sre_SpeechRecognized;
        Choices command = new Choices();
        command.Add("Старт");
        command.Add("Стоп");
        command.Add("Камера");
        command.Add("Глубина");
        GrammarBuilder gb = new GrammarBuilder();
        gb.Append(command);
        Grammar g = new Grammar(gb);
        sre.LoadGrammar(g);
        sre.RecognizeAsync(RecognizeMode.Multiple);
    }

    private void Sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        if (e.Result.Confidence > 0.8 && e.Result.Text == "Старт")
        {
            sp.Stream = Properties.Resources.start;
            sp.Play();
            if (KinectSensor.KinectSensors.Count > 0)
            {
                mysensor = KinectSensor.KinectSensors[0];
                this.StartSensor();
                //port.Open();
                mysensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                mysensor.SkeletonStream.Enable();
                mysensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Default;
                lbl.Text = "1";

            }
            else
            {
                MessageBox.Show("No device!");
                this.Close();
            }
        }
        if (e.Result.Confidence > 0.8 && e.Result.Text == "Стоп")
        {
            sp.Stream = Properties.Resources.stop;
            sp.Play();
            if (mysensor != null && mysensor.IsRunning)
            {
                mysensor.Stop();                    
            }
        }
        if (e.Result.Confidence > 0.8 && e.Result.Text == "Камера")
        {
            this.mysensor.ColorFrameReady += Mysensor_ColorFrameReady;
        }
    }

    private void Mysensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
    {
        using (ColorImageFrame imageSensorFrame = e.OpenColorImageFrame())
        {
            if (imageSensorFrame == null)
            {
                return;
            }
            Color.Image = imageSensorFrame.ToBitmap();
            lbl.Text = 2.ToString();
            

        }
    }

    private void StartSensor()
    {
        if (this.mysensor != null && !this.mysensor.IsRunning)
        {
            this.mysensor.Start();

        }
    }
}

}


Ответы (1 шт):

Автор решения: Maxim

Решить получилось асинхронным вызовом обработчика распознавания голоса.

→ Ссылка