Is it possible to sign a lib event on a REST request and wait for the return?

Asked

Viewed 24 times

0

I wonder if it is possible to sign an event from a third party lib, which in the case is Microsoft.Speech.dll. Because, in my attempt, it did not work very well, ie, recognition does not happen and the event is not fired. I tried using Task and also without, but I did not succeed. I don’t really know if it is possible. sign an event in this way and the request wait. Thanks in advance for the attention and possible help. Follow the code.

    private string retorno = string.Empty;

    public async Task<string> Get()
    {
        var result = Task.Run(() =>
        {
            var comandos = new string[] { "comando 1", "comando 2", "comando N" };
            var ci = new CultureInfo("pt-BR");
            var sre = new SpeechRecognitionEngine(ci);
            var gramatica = new Choices();
            gramatica.Add(comandos);
            var gb = new GrammarBuilder(gramatica);
            var g = new Grammar(gb);
            sre.RequestRecognizerUpdate();
            sre.LoadGrammarAsync(g);
            sre.SpeechRecognized += Sre_SpeechRecognized;

            sre.SetInputToWaveFile("anyWavfile.wav");
            sre.RecognizeAsync(RecognizeMode.Multiple);

        });

        await Task.WhenAll(result);

        return retorno;
    }

    private void Sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        retorno += e.Result.Text;
    }
  • Explain better what you intend to do, not missing a await no RecognizeAsync() or on the call of your Get()?

  • I ended up solving gave another way, holding the Thread until the event ends. These methods do not accept await, it is a very old lib. Thanks!

  • It would be nice if you posted an answer to how you solved the question or closed the question to not leave it open.

1 answer

0


Follow the solution I applied

private string text = string.Empty;
private ManualResetEvent manualResetEvent = new ManualResetEvent(false);

public string Get()
{
    var result = Task.Run(() =>
    {
        //code...

    });

    manualResetEvent.WaitOne();   

    return text;
}

private void Sre_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
{
    manualResetEvent.Set();
}

private void Sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    text += e.Result.Text;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.