Thread and task with static method

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;
    }
    
asked by anonymous 30.01.2018 / 00:25

2 answers

0

Good luck, I solved the problem in the following way.

I took the static method was just an async common method, and to get the return method I used the invoker function in the text box, to send the return of the task as I wanted

    
19.02.2018 / 08:54
0

You can simply retrieve the Result of the call from your 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;
    }
}
    
30.01.2018 / 15:01