Forum Xamarin.iOS
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.

Problem creating overlay

Hi,

I am trying to add a loading overlay whilst my app makes a call to to web api service but for some reason it doesnt work. When i use the following code, it works as expected:

`Task.Factory.StartNew (

                () => {
                    Console.WriteLine ( "Hello from taskA." );
                    System.Threading.Thread.Sleep(3000);
                }

            ).ContinueWith ( 
                t => {
                    loadingOverlay.Hide();

                }, TaskScheduler.FromCurrentSynchronizationContext()
            );`

The above code writes into the console and then sleeps for 3 seconds as expected and then removes the overlay. However, when i add a call into the repository, it skips the entire first section and just hides the overlay.

The new code looks like this:

            `Task.Factory.StartNew (

                () => {
                    Console.WriteLine ( "Hello from taskA." );
                    u = _userRepository.GetUserByEmail(EmailTextBox.Text);
                    System.Threading.Thread.Sleep(3000);
                }
                ).ContinueWith ( 
                t => {
                    loadingOverlay.Hide();

                }, TaskScheduler.FromCurrentSynchronizationContext()
            );


        };`

The code in the repository is as follows:

public User GetUserByEmail(string email)
{
using (var client = new HttpClient ()) {
client.BaseAddress = _uri;
client.DefaultRequestHeaders.Accept.Clear ();
client.DefaultRequestHeaders.Accept.Add (new MediaTypeWithQualityHeaderValue ("application/json"));

            HttpResponseMessage response = client.GetAsync(String.Format("GetUser?email={0}", email)).Result;
            if (response.IsSuccessStatusCode) {

                var jsonResponse = response.Content.ReadAsStringAsync ().Result;
                return JsonConvert.DeserializeObject<User> (jsonResponse);

            } else {
                return null;
            }
        }

    }

Any help would be appreciated - thanks in advance :)

Sign In or Register to comment.