Thread and task with static method

Asked

Viewed 72 times

0

How do I return the result of this method to a textbox

static async Task<object> StreamingRecognizeAsync(string filePath)
    {
        var speech = SpeechClient.Create();
        var streamingCall = speech.StreamingRecognize();
        String saidWhat, lastSaidWhat = null;
        // Write the initial request with the config.
        await streamingCall.WriteAsync(
            new StreamingRecognizeRequest()
            {
                StreamingConfig = new StreamingRecognitionConfig()
                {
                    Config = new RecognitionConfig()
                    {
                        Encoding =
                        RecognitionConfig.Types.AudioEncoding.Linear16,
                        SampleRateHertz = 16000,
                        LanguageCode = "pt-br",
                    },
                    InterimResults = true,
                }
            });
        // Print responses as they arrive.
        Task printResponses = Task.Run(async () =>
        {
            while (await streamingCall.ResponseStream.MoveNext(
                default(CancellationToken)))
            {
                foreach (var result in streamingCall.ResponseStream
                    .Current.Results)
                {
                    foreach (var alternative in result.Alternatives)
                    {
                        saidWhat = alternative.Transcript;
                        if (lastSaidWhat != saidWhat)
                        {
                            Console.WriteLine(saidWhat);
                            lastSaidWhat = saidWhat;
                            PegaTexto(saidWhat);
                            //Need to call this on UI thread ....
                            //textBox1.Invoke((MethodInvoker)delegate { textBox1.AppendText(textBox1.Text + saidWhat + " \r\n"); });
                        }
                    }
                }
            }
        });
        // Stream the file content to the API.  Write 2 32kb chunks per
        // second.
        using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
        {
            var buffer = new byte[32 * 1024];
            int bytesRead;
            while ((bytesRead = await fileStream.ReadAsync(
                buffer, 0, buffer.Length)) > 0)
            {
                await streamingCall.WriteAsync(
                    new StreamingRecognizeRequest()
                    {
                        AudioContent = Google.Protobuf.ByteString
                        .CopyFrom(buffer, 0, bytesRead),
                    });
                await Task.Delay(500);
            };
        }
        await streamingCall.WriteCompleteAsync();
        await printResponses;
        return 0;
    }

2 answers

0


Good guys I managed to solve the problem the following way.

I took the method of Static was just a common async method, and to catch the return of the method I used the function Invoker in the text box, to send the return of the task as I wanted

0

You can simply retrieve the Result call of its asynchronous method;

string filePath = "";
var result = StreamingRecognizeAsync(filePath).Result;

Example:

class ConsoleApp
{

    static void Main(string[] args)
    {

        var result = TaskAsync(5).Result;        
        Console.Write(result);
        Console.ReadLine();
    }

    static async Task<int> TaskAsync(int input)
    {
        int retorno = input;
        await Task.Delay(2000);
        return retorno;
    }
}
  • when I put . result does not work my way

  • What error is presented?

Browser other questions tagged

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