Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

App freezing whilst using ProgressDialog

Hey all,

I'm trying to run a ProgressDialog whilst retrieving data through an async REST call, but it freezes the screen sometimes.
I was wondering if I could run it on a separate thread, maybe the UI thread(I know very little about threading)?

Thanks for any help!!

This is my method:

protected async override void OnResume ()
{
    try
    {
        base.OnResume ();

        var progressDialog = ProgressDialog.Show(this, "", "Loading Workshops...");
        await GetWorkshops();
        progressDialog.Dismiss();
    }
    catch (Exception e)
    {
        new AlertDialog.Builder (this)
            .SetMessage(e.Message + "\n" + e.StackTrace)
            .SetTitle("Application Error")
            .Show();
    }
}

Answers

  • rzee7rzee7 INUniversity ✭✭✭✭✭
    edited October 2015

    Hi @RohanWilliams

    Can you please explore following method?

    GetWorkshops();
    

    Thanks
    RIYAZ

  • @rzee7 Sorry, it does this:

    public async Task<bool> GetWorkshops()
    {
        WrkSets = await RESTClass.GetWorkshopList("?active=true");  
    
        // create our adapter
        taskList = new Adapters.TaskListAdapter(this, WrkSets);
    
        //Hook up our adapter to our ListView
        lvWorkShops.Adapter = taskList;
    
        return true;
    }
    
    public static async Task<List<WorkshopSets>> GetWorkshopList(String strParameters = "")
    {
        String result = await GetRESTCall("/workshop/workshopSets", strParameters);
        RootWorkshopSets _RootObj = new RootWorkshopSets(result);
        return _RootObj.Results;
    }
    
    private static async Task<String> GetRESTCall(String strCall, String strParameters = "")
    {
        using (HttpClient _HttpClient = new HttpClient())
        {
            _HttpClient.Timeout = new TimeSpan(0, 0, 1, 0);
            using (HttpRequestMessage _HttpRequest = new HttpRequestMessage(HttpMethod.Get, "http://MyWebsite/api" + strCall + "/" + strParameters))
            {
                _HttpRequest.Headers.Add("AppKey", "123456");
    
                using (HttpResponseMessage _HttpResponse = await _HttpClient.SendAsync(_HttpRequest))
                {
                    if (_HttpResponse.IsSuccessStatusCode)
                    {
                        String strTemp = await _HttpResponse.Content.ReadAsStringAsync();
                        return strTemp;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }
    }
    
  • rzee7rzee7 INUniversity ✭✭✭✭✭

    Thank you @RohanWilliams

    Looks good here!! Just wanted to see the place where you are deserializing your download data; Sorry but I doubt could be here:

     new RootWorkshopSets(result);
    

    Any idea, about where are you using Json NewtonSoft to deserialize data?

  • @rzee7 does running this async task influence the ProgressDialogue running because it's being awaited on a different thread?
    Also I think this is what your looking for?

    public class RootWorkshopSets
    {
        public RootWorkshopSets(String strJson)
        {
            try
            {
                JObject jObject = JObject.Parse(strJson);
                Results = new List<WorkshopSets>();
                foreach (var d in jObject["Results"].Children()) 
                { 
                    Results.Add(new WorkshopSets((int)d["id"], (String)d["name"], (String)d["archived"]));
                }
            }
            catch 
            {
                throw;
            }
        }
    
        public List<WorkshopSets> Results { get; set; }
        public bool IsSuccess { get; set; }
        public object DisplayMessage { get; set; }
    }
    
Sign In or Register to comment.