how can an async method be executed synchronously in Xamarin.Forms
up vote
1
down vote
favorite
I am building a screen for my app in xamarin.forms the caul is based on a tabbedpage which is built dynamically based on a list of objects which I get as a result of consuming a service.
After I call the method to consume the API that brings the list, I need to go through it based on certain data of it to fill an observable collection of viewmodels, which will be the tabs. The problem I have is that I do not know how to call the async method that consumes the API in a synchronized way so that the consumption of the API does not conflict with the operation of going through the list.
Then a fraction of the code of my ViewModel:
public MonitoringViewModel()
{
LoadThings();
Tabs = new ObservableCollection<MonitoringTabsViewModel>();
foreach (PcThing t in Things)
{
Tabs.Add(new MonitoringTabsViewModel(t.description));
}
}
private async void LoadThings()
{
Things = new List<PcThing>(await App.WebApiManager.GetCustomerThinksAsync());
}
What I get is that in xamarin live player the app after a few seconds go from the green signal to the red one without showing anything, and in the log of it I get this:
Target of GetEnumerator is null (NullReferenceException)
c# .net xamarin.forms async-await tabbedpage
add a comment |
up vote
1
down vote
favorite
I am building a screen for my app in xamarin.forms the caul is based on a tabbedpage which is built dynamically based on a list of objects which I get as a result of consuming a service.
After I call the method to consume the API that brings the list, I need to go through it based on certain data of it to fill an observable collection of viewmodels, which will be the tabs. The problem I have is that I do not know how to call the async method that consumes the API in a synchronized way so that the consumption of the API does not conflict with the operation of going through the list.
Then a fraction of the code of my ViewModel:
public MonitoringViewModel()
{
LoadThings();
Tabs = new ObservableCollection<MonitoringTabsViewModel>();
foreach (PcThing t in Things)
{
Tabs.Add(new MonitoringTabsViewModel(t.description));
}
}
private async void LoadThings()
{
Things = new List<PcThing>(await App.WebApiManager.GetCustomerThinksAsync());
}
What I get is that in xamarin live player the app after a few seconds go from the green signal to the red one without showing anything, and in the log of it I get this:
Target of GetEnumerator is null (NullReferenceException)
c# .net xamarin.forms async-await tabbedpage
Please review this SO topic. We use it in Production and all works very well.
– Alexander I.
Nov 18 at 21:05
Best way to handle asynchronous code is to make the synchronous code async. Usually there is some kind of event that starts the process that you can mark as async void. As for calling asynchronous code in a constructor, maby this reply to a similar scenario could help?
– Shazi
Nov 18 at 22:07
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am building a screen for my app in xamarin.forms the caul is based on a tabbedpage which is built dynamically based on a list of objects which I get as a result of consuming a service.
After I call the method to consume the API that brings the list, I need to go through it based on certain data of it to fill an observable collection of viewmodels, which will be the tabs. The problem I have is that I do not know how to call the async method that consumes the API in a synchronized way so that the consumption of the API does not conflict with the operation of going through the list.
Then a fraction of the code of my ViewModel:
public MonitoringViewModel()
{
LoadThings();
Tabs = new ObservableCollection<MonitoringTabsViewModel>();
foreach (PcThing t in Things)
{
Tabs.Add(new MonitoringTabsViewModel(t.description));
}
}
private async void LoadThings()
{
Things = new List<PcThing>(await App.WebApiManager.GetCustomerThinksAsync());
}
What I get is that in xamarin live player the app after a few seconds go from the green signal to the red one without showing anything, and in the log of it I get this:
Target of GetEnumerator is null (NullReferenceException)
c# .net xamarin.forms async-await tabbedpage
I am building a screen for my app in xamarin.forms the caul is based on a tabbedpage which is built dynamically based on a list of objects which I get as a result of consuming a service.
After I call the method to consume the API that brings the list, I need to go through it based on certain data of it to fill an observable collection of viewmodels, which will be the tabs. The problem I have is that I do not know how to call the async method that consumes the API in a synchronized way so that the consumption of the API does not conflict with the operation of going through the list.
Then a fraction of the code of my ViewModel:
public MonitoringViewModel()
{
LoadThings();
Tabs = new ObservableCollection<MonitoringTabsViewModel>();
foreach (PcThing t in Things)
{
Tabs.Add(new MonitoringTabsViewModel(t.description));
}
}
private async void LoadThings()
{
Things = new List<PcThing>(await App.WebApiManager.GetCustomerThinksAsync());
}
What I get is that in xamarin live player the app after a few seconds go from the green signal to the red one without showing anything, and in the log of it I get this:
Target of GetEnumerator is null (NullReferenceException)
c# .net xamarin.forms async-await tabbedpage
c# .net xamarin.forms async-await tabbedpage
asked Nov 18 at 20:44
lavilaso
143
143
Please review this SO topic. We use it in Production and all works very well.
– Alexander I.
Nov 18 at 21:05
Best way to handle asynchronous code is to make the synchronous code async. Usually there is some kind of event that starts the process that you can mark as async void. As for calling asynchronous code in a constructor, maby this reply to a similar scenario could help?
– Shazi
Nov 18 at 22:07
add a comment |
Please review this SO topic. We use it in Production and all works very well.
– Alexander I.
Nov 18 at 21:05
Best way to handle asynchronous code is to make the synchronous code async. Usually there is some kind of event that starts the process that you can mark as async void. As for calling asynchronous code in a constructor, maby this reply to a similar scenario could help?
– Shazi
Nov 18 at 22:07
Please review this SO topic. We use it in Production and all works very well.
– Alexander I.
Nov 18 at 21:05
Please review this SO topic. We use it in Production and all works very well.
– Alexander I.
Nov 18 at 21:05
Best way to handle asynchronous code is to make the synchronous code async. Usually there is some kind of event that starts the process that you can mark as async void. As for calling asynchronous code in a constructor, maby this reply to a similar scenario could help?
– Shazi
Nov 18 at 22:07
Best way to handle asynchronous code is to make the synchronous code async. Usually there is some kind of event that starts the process that you can mark as async void. As for calling asynchronous code in a constructor, maby this reply to a similar scenario could help?
– Shazi
Nov 18 at 22:07
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Since you are doing this in the constructor , I would try the following:
using System.Threading.Tasks;
The risk here is if you are not in control of the LoadThings completing, it can hang.
public MonitoringViewModel()
{
var task = Task.Run(async () => { await LoadThings();}
Task.WaitAll(task); //block and wait for task to complete
WhyTask.WaitAll
?
– Paulo Morgado
Nov 18 at 23:39
Thank you very much, after many days fighting with this, your answer is the only thing that has worked for me, although I am concerned about the risk you are aiming at, could I please give some more reference on this and / or explain it in a little more detail?
– lavilaso
Nov 18 at 23:52
Task.WaitAll is just what Microsoft called it (may not be the best name), but basically it waits for all of the provided tasks to complete execution. You can pass it a single task (as I done above) or an array of tasks, but it will block any further progress until the associated task(s) have ended.
– cylon
Nov 19 at 16:32
Thanks, do you have any examples of how to do this with an exception control and timeout to control a time limit of completion of the task?
– lavilaso
Nov 21 at 17:45
add a comment |
up vote
0
down vote
public async Task<List<PcThing>> LoadThings()
{
return await App.WebApiManager.GetCustomerThinksAsync();
}
And in your ViewModel
Things = LoadThings().GetAwaiter().GetResult();
Thanks, although it does not work, I just tried it and I get the same result as when I tried to use App.ApiManager.GetCustomerThinksAsync (). Result, which is that the green signal in the xamarin live player never disappears and never starts the app and verifies With the breakpoints the app never executes the GetCutomerThinks method, I get to see the flow on it but the breakpoints within it are never reached. Any ideas ?
– lavilaso
Nov 18 at 21:29
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Since you are doing this in the constructor , I would try the following:
using System.Threading.Tasks;
The risk here is if you are not in control of the LoadThings completing, it can hang.
public MonitoringViewModel()
{
var task = Task.Run(async () => { await LoadThings();}
Task.WaitAll(task); //block and wait for task to complete
WhyTask.WaitAll
?
– Paulo Morgado
Nov 18 at 23:39
Thank you very much, after many days fighting with this, your answer is the only thing that has worked for me, although I am concerned about the risk you are aiming at, could I please give some more reference on this and / or explain it in a little more detail?
– lavilaso
Nov 18 at 23:52
Task.WaitAll is just what Microsoft called it (may not be the best name), but basically it waits for all of the provided tasks to complete execution. You can pass it a single task (as I done above) or an array of tasks, but it will block any further progress until the associated task(s) have ended.
– cylon
Nov 19 at 16:32
Thanks, do you have any examples of how to do this with an exception control and timeout to control a time limit of completion of the task?
– lavilaso
Nov 21 at 17:45
add a comment |
up vote
1
down vote
Since you are doing this in the constructor , I would try the following:
using System.Threading.Tasks;
The risk here is if you are not in control of the LoadThings completing, it can hang.
public MonitoringViewModel()
{
var task = Task.Run(async () => { await LoadThings();}
Task.WaitAll(task); //block and wait for task to complete
WhyTask.WaitAll
?
– Paulo Morgado
Nov 18 at 23:39
Thank you very much, after many days fighting with this, your answer is the only thing that has worked for me, although I am concerned about the risk you are aiming at, could I please give some more reference on this and / or explain it in a little more detail?
– lavilaso
Nov 18 at 23:52
Task.WaitAll is just what Microsoft called it (may not be the best name), but basically it waits for all of the provided tasks to complete execution. You can pass it a single task (as I done above) or an array of tasks, but it will block any further progress until the associated task(s) have ended.
– cylon
Nov 19 at 16:32
Thanks, do you have any examples of how to do this with an exception control and timeout to control a time limit of completion of the task?
– lavilaso
Nov 21 at 17:45
add a comment |
up vote
1
down vote
up vote
1
down vote
Since you are doing this in the constructor , I would try the following:
using System.Threading.Tasks;
The risk here is if you are not in control of the LoadThings completing, it can hang.
public MonitoringViewModel()
{
var task = Task.Run(async () => { await LoadThings();}
Task.WaitAll(task); //block and wait for task to complete
Since you are doing this in the constructor , I would try the following:
using System.Threading.Tasks;
The risk here is if you are not in control of the LoadThings completing, it can hang.
public MonitoringViewModel()
{
var task = Task.Run(async () => { await LoadThings();}
Task.WaitAll(task); //block and wait for task to complete
answered Nov 18 at 22:55
cylon
113
113
WhyTask.WaitAll
?
– Paulo Morgado
Nov 18 at 23:39
Thank you very much, after many days fighting with this, your answer is the only thing that has worked for me, although I am concerned about the risk you are aiming at, could I please give some more reference on this and / or explain it in a little more detail?
– lavilaso
Nov 18 at 23:52
Task.WaitAll is just what Microsoft called it (may not be the best name), but basically it waits for all of the provided tasks to complete execution. You can pass it a single task (as I done above) or an array of tasks, but it will block any further progress until the associated task(s) have ended.
– cylon
Nov 19 at 16:32
Thanks, do you have any examples of how to do this with an exception control and timeout to control a time limit of completion of the task?
– lavilaso
Nov 21 at 17:45
add a comment |
WhyTask.WaitAll
?
– Paulo Morgado
Nov 18 at 23:39
Thank you very much, after many days fighting with this, your answer is the only thing that has worked for me, although I am concerned about the risk you are aiming at, could I please give some more reference on this and / or explain it in a little more detail?
– lavilaso
Nov 18 at 23:52
Task.WaitAll is just what Microsoft called it (may not be the best name), but basically it waits for all of the provided tasks to complete execution. You can pass it a single task (as I done above) or an array of tasks, but it will block any further progress until the associated task(s) have ended.
– cylon
Nov 19 at 16:32
Thanks, do you have any examples of how to do this with an exception control and timeout to control a time limit of completion of the task?
– lavilaso
Nov 21 at 17:45
Why
Task.WaitAll
?– Paulo Morgado
Nov 18 at 23:39
Why
Task.WaitAll
?– Paulo Morgado
Nov 18 at 23:39
Thank you very much, after many days fighting with this, your answer is the only thing that has worked for me, although I am concerned about the risk you are aiming at, could I please give some more reference on this and / or explain it in a little more detail?
– lavilaso
Nov 18 at 23:52
Thank you very much, after many days fighting with this, your answer is the only thing that has worked for me, although I am concerned about the risk you are aiming at, could I please give some more reference on this and / or explain it in a little more detail?
– lavilaso
Nov 18 at 23:52
Task.WaitAll is just what Microsoft called it (may not be the best name), but basically it waits for all of the provided tasks to complete execution. You can pass it a single task (as I done above) or an array of tasks, but it will block any further progress until the associated task(s) have ended.
– cylon
Nov 19 at 16:32
Task.WaitAll is just what Microsoft called it (may not be the best name), but basically it waits for all of the provided tasks to complete execution. You can pass it a single task (as I done above) or an array of tasks, but it will block any further progress until the associated task(s) have ended.
– cylon
Nov 19 at 16:32
Thanks, do you have any examples of how to do this with an exception control and timeout to control a time limit of completion of the task?
– lavilaso
Nov 21 at 17:45
Thanks, do you have any examples of how to do this with an exception control and timeout to control a time limit of completion of the task?
– lavilaso
Nov 21 at 17:45
add a comment |
up vote
0
down vote
public async Task<List<PcThing>> LoadThings()
{
return await App.WebApiManager.GetCustomerThinksAsync();
}
And in your ViewModel
Things = LoadThings().GetAwaiter().GetResult();
Thanks, although it does not work, I just tried it and I get the same result as when I tried to use App.ApiManager.GetCustomerThinksAsync (). Result, which is that the green signal in the xamarin live player never disappears and never starts the app and verifies With the breakpoints the app never executes the GetCutomerThinks method, I get to see the flow on it but the breakpoints within it are never reached. Any ideas ?
– lavilaso
Nov 18 at 21:29
add a comment |
up vote
0
down vote
public async Task<List<PcThing>> LoadThings()
{
return await App.WebApiManager.GetCustomerThinksAsync();
}
And in your ViewModel
Things = LoadThings().GetAwaiter().GetResult();
Thanks, although it does not work, I just tried it and I get the same result as when I tried to use App.ApiManager.GetCustomerThinksAsync (). Result, which is that the green signal in the xamarin live player never disappears and never starts the app and verifies With the breakpoints the app never executes the GetCutomerThinks method, I get to see the flow on it but the breakpoints within it are never reached. Any ideas ?
– lavilaso
Nov 18 at 21:29
add a comment |
up vote
0
down vote
up vote
0
down vote
public async Task<List<PcThing>> LoadThings()
{
return await App.WebApiManager.GetCustomerThinksAsync();
}
And in your ViewModel
Things = LoadThings().GetAwaiter().GetResult();
public async Task<List<PcThing>> LoadThings()
{
return await App.WebApiManager.GetCustomerThinksAsync();
}
And in your ViewModel
Things = LoadThings().GetAwaiter().GetResult();
answered Nov 18 at 21:03
Bruno Caceiro
1,97211121
1,97211121
Thanks, although it does not work, I just tried it and I get the same result as when I tried to use App.ApiManager.GetCustomerThinksAsync (). Result, which is that the green signal in the xamarin live player never disappears and never starts the app and verifies With the breakpoints the app never executes the GetCutomerThinks method, I get to see the flow on it but the breakpoints within it are never reached. Any ideas ?
– lavilaso
Nov 18 at 21:29
add a comment |
Thanks, although it does not work, I just tried it and I get the same result as when I tried to use App.ApiManager.GetCustomerThinksAsync (). Result, which is that the green signal in the xamarin live player never disappears and never starts the app and verifies With the breakpoints the app never executes the GetCutomerThinks method, I get to see the flow on it but the breakpoints within it are never reached. Any ideas ?
– lavilaso
Nov 18 at 21:29
Thanks, although it does not work, I just tried it and I get the same result as when I tried to use App.ApiManager.GetCustomerThinksAsync (). Result, which is that the green signal in the xamarin live player never disappears and never starts the app and verifies With the breakpoints the app never executes the GetCutomerThinks method, I get to see the flow on it but the breakpoints within it are never reached. Any ideas ?
– lavilaso
Nov 18 at 21:29
Thanks, although it does not work, I just tried it and I get the same result as when I tried to use App.ApiManager.GetCustomerThinksAsync (). Result, which is that the green signal in the xamarin live player never disappears and never starts the app and verifies With the breakpoints the app never executes the GetCutomerThinks method, I get to see the flow on it but the breakpoints within it are never reached. Any ideas ?
– lavilaso
Nov 18 at 21:29
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365274%2fhow-can-an-async-method-be-executed-synchronously-in-xamarin-forms%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please review this SO topic. We use it in Production and all works very well.
– Alexander I.
Nov 18 at 21:05
Best way to handle asynchronous code is to make the synchronous code async. Usually there is some kind of event that starts the process that you can mark as async void. As for calling asynchronous code in a constructor, maby this reply to a similar scenario could help?
– Shazi
Nov 18 at 22:07