1

Im building a new app and since i want it to be smooth as everyone, I want to use a background thread that would be responsible for all the data downloading using restsharp. Im also following the MVVM pattern. I've been reading a lot about task.run and how to use it properly and the whole async-await topic. But since Im new to all this, Im not sure how I should procceed to do things right. I have a lot of code so I will breifly try to explain what Im doing and then put a snippet.

So I started with creating a service class that contains all the functions that are using restsharp to get the data. And inside my ViewModel Im calling those functions in the very begining. Im trying to use tasks and run those functions on the background thread but the app get blocked on the splash screen. And abviously thats because Im doing things wrong ... so I decided to ask you guys.

I have this function for exemple :

public string GetResPor()
{
    var restClient = new RestClient { BaseUrl = new Uri("http://xxx.xxx.xxx.xxx:xxxx") };
    var request = new RestRequest
    {
        Resource = "getCliPor",
        Method = Method.GET
    };
    request.AddParameter(new Parameter { Name = "idt", Value = GetImAsync().GetAwaiter().GetResult(), Type = ParameterType.GetOrPost });
    var result = restClient.Execute(request);
    Port = result.Content;
    return Port;
}

When I convert this on a Task :

public async Task<string> GetResPor()
{
    var restClient = new RestClient { BaseUrl = new Uri("http://xxx.xxx.xxx.xxx:xxxx") };
    var request = new RestRequest
    {
        Resource = "getCliPor",
        Method = Method.GET
    };
    request.AddParameter(new Parameter { Name = "idt", Value = GetImAsync().GetAwaiter().GetResult(), Type = ParameterType.GetOrPost });
    var result = await restClient.ExecuteTaskAsync(request);
    Port = result.Content;
    return Port;
}

on the ViewModel I start by creating a new instance of my service class and then:

Port = RD.GetRestauPort().GetAwaiter().GetResult();

And this is where the app get blocked, no exceptions no nothing.

Mihail Duchev
  • 4,691
  • 10
  • 25
  • 32
Shahid Od
  • 103
  • 4
  • 13
  • async/await is NOT the same thing as using a background task/thread – Jason May 13 '20 at 19:33
  • @Jason thank u for your quick reply. I agree, that's why Im asking, how can i use a backgroud thread ? and is the idea of running all data downloading on a another thread helpful ? do you have any links that can help me out ? – Shahid Od May 13 '20 at 20:06
  • https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming – Jason May 13 '20 at 20:09

1 Answers1

1

To keep things simple, let's start with the basics. The easiest thing to do, in order to run something in a background thread, is to call it inside a Task.Run(). What this does is:

Queues the specified work to run on the ThreadPool and returns a task or Task<TResult> handle for that work.

Basically, you are delegating your work to the TreadPool and it handles everything for you - looks for a worker, waits for the worker to finish its job (on a new thread) and then notifies you of the result.

So, basically, whatever you want to be in a background thread, the simples solution will be to wrap it inside a Task.Run() and await its result, in case you need it.

Also, avoid using GetAwaiter().GetResult(). The simple rule in asynchronous programming is - if you can await, await all the way up.

You can read more about the topics in

  1. this SO post
  2. Advanced Tips for Using Task.Run With Async/Await
  3. Using Task.Run in Conjunction with Async/Await
Mihail Duchev
  • 4,691
  • 10
  • 25
  • 32