I
I
IvankoPo2018-08-22 17:57:21
WPF
IvankoPo, 2018-08-22 17:57:21

Why is the method executed on the main thread?

public partial class MainWindow : Window
    {

        public async Task<List<String>> getDataa()
        {
            var client = new RestClient("http://url");
            var request = new RestRequest("getTopics/", Method.POST);
            var path = @"D:\Dataset\entertainment_news_article_10355.txt";
            request.AddFile(path, path);
            var response2 = await client.ExecuteTaskAsync<List<String>>(request);
            return response2.Data;
        }
        public MainWindow()
        {
            InitializeComponent();
            BT.Click += async (sender, e) =>
            {
                List<String> res = await getDataa();
                LB.ItemsSource = res;
            };
        }

Why is the getData method running on the main thread if methods with async.
How to change the code to getDataa(); called asynchronously?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ApeCoder, 2018-08-22
@ApeCoder

Asynchronous does not mean multithreaded.
https://docs.microsoft.com/en-us/dotnet/csharp/async

If your task is CPU bound and responsiveness is important to you, use async and await, but move the task execution to another thread with the Task.Run construct. If parallelism is applicable to the task, you can also consider using the Task Parallel Library.

A
Alexander Yudakov, 2018-08-23
@AlexanderYudakov

An obvious bug in the library you are using. If the method that performs the network operation is declared by the authors as asynchronous, then there should be no blocking of the calling thread.
To avoid such surprises, I do not use third-party libraries. Yes, you have to do more dirty work with your hands. But, as a rule, all this is done once, after which it is easily reused.
Now you will have to rake someone else's shit code from the github with your hands. The big question is who will spend more time - you or me?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question