×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Making a synchronous web request from UI thread.

    Hi,
    I have a requirement of making synchronous rest call from UI thread. I know making a synchronout web call is not good but its a must requirement in my case.
    I tried using AutoResetEvent with BackgroundWorker and WebClient but since AutoResetEvent.WaitOne() is on the UI thread it completely freezes UI as callbacks from asynchronous network operations are queued on the UI thread and calling AutoResetEvent.WaitOne() on it makes network call backs never arrive.

    Kindly provide a solution for this.

    Regards,
    Vinay

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    542

    Re: Making a synchronous web request from UI thread.

    Quote Originally Posted by vinayppatil View Post
    Hi,
    I have a requirement of making synchronous rest call from UI thread. I know making a synchronout web call is not good but its a must requirement in my case.
    Why do you have to make a synchronous (and blocking) call on the UI thread? what is the technical reason?

  3. #3
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: Making a synchronous web request from UI thread.

    Hi theothernt,
    Quote Originally Posted by theothernt View Post
    Why do you have to make a synchronous (and blocking) call on the UI thread? what is the technical reason?
    I am matching current app version with latest app version on server. And user must be not be allowed to use the app if a higher version is available. So i want to wait for the server response before proceeding further. Hope you got my point.

    Regards,
    Vinay

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,674

    Re: Making a synchronous web request from UI thread.

    If you have a splash/loading screen, you could just leave it active while doing the check. If you do not have such screen, you can add one, or disable the UI elements while checking, or just ignore whatever the user is doing (though this latter one may result in a lot of 'if'-s). Be sure to check if you can really expect always-available network connection in WP apps, it may result in rejection from the store.

  5. #5
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    542

    Re: Making a synchronous web request from UI thread.

    And here's a tutorial on how to make a Splash Screen...

    http://www.geekchamp.com/articles/al...-splash-screen

  6. #6
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: Making a synchronous web request from UI thread.

    Quote Originally Posted by wizard_hu_ View Post
    If you have a splash/loading screen, you could just leave it active while doing the check. If you do not have such screen, you can add one, or disable the UI elements while checking, or just ignore whatever the user is doing (though this latter one may result in a lot of 'if'-s).
    Hmmmm. So i just need to make use of guards until i am done with version matching. There is no way platform provides for synchronized rest calls?
    And i do have a splash screen but that's just an image and i will need to refactor a lot of code for adding one as theothernt suggested.

    Quote Originally Posted by wizard_hu_ View Post
    Be sure to check if you can really expect always-available network connection in WP apps, it may result in rejection from the store.
    Yes i have handled this case.

    Regards,
    Vinay

  7. #7
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: Making a synchronous web request from UI thread.

    Quote Originally Posted by theothernt View Post
    And here's a tutorial on how to make a Splash Screen...

    http://www.geekchamp.com/articles/al...-splash-screen
    Thanks for the link but adding a splash screen now will incur a lot of rework so i was wondering if there is a synchronized way to make rest calls. BTW i had included a splash screen in my article here. Anyways handling it by using if's seems an easy way. Lets see if i can get something more easier. Do post here if you find something.

    Regards,
    Vinay

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: Making a synchronous web request from UI thread.

    Sliverlight, Windows Phone and Windows RT were purposely made to not allow what you are asking for and the reason for it is because it's the wrong way to do things. The UI thread is only for user interaction.

    You can call it whatever you want to, but just replace your start page with a page with a progress bar and do the call in the background as it should. When you get the result (or not - what happens if there's no internet access?) you either navigate to the page that you have now as startup or a page explaining why you can't use the application.

    Besides that right way to do it, there's a mandatory up to 5 seconds to load the first page in order to pass store certification.

  9. #9
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: Making a synchronous web request from UI thread.

    Hi paulo,

    Thanks for your response. Maybe i was not clear enough but what i asked for is synchronization on UI thread and not blocking the UI thread. I raised this question for the very reason of getting blocked on UI thread after calling AutoResetEvent.WaitOne(). Anyways the solution i implemented is
    1. Delegate the rest call for checking app version to a webclient using BackgroundWorker in your UI thread. Webclient uses AutoResetEvent to synchronize rest call.
    2. Show a progress bar on top of your page.
    3. Depending on the rest call results inside BackgroundWorker.RunWorkerCompleted() take required actions and hide the progress bar.

    Code:
    Constructor()
    {
    	//Show progress bar
    	InvokeCheckForAppUpdate();
    }
    
    public void InvokeCheckForAppUpdate()
    {
    	_bgAppUpdateLog = new BackgroundWorker();
    	_bgAppUpdateLog.DoWork += new DoWorkEventHandler(bg_bgAppUpdateLog_DoWork);
    	_bgAppUpdateLog.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_bgAppUpdateLog_RunWorkerCompleted);
    	_bgAppUpdateLog.RunWorkerAsync();
    }
    
    private void bg_bgAppUpdateLog_DoWork(object sender, DoWorkEventArgs e)
    {
    	_isAppUpdateAvailable = ServiceManager.ServiceManagerInstance.CheckForAppUpdate();
    }
    		
    private void bg_bgAppUpdateLog_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    	if (_isAppUpdateAvailable)
    	{
    		// take required actions
    	}
    	//hide the progress bar
    }
    
    
    //ServiceManager class
    	.
    	.
    	.
    
    
    public bool CheckForAppUpdate()
    {
    	appUpdateAutoResetEvent = new AutoResetEvent(false);
    	WebClient webClient = new WebClient();
    	.
    	.
    	.
    	webClient.UploadStringAsync(uri, "POST", postData.ToString(), appUpdateAutoResetEvent);
    
    	appUpdateAutoResetEvent.WaitOne();
    	return _isAppUpdateAvailable;
    }
    
    private void appUdateWebClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
    	try
    	{
    		if (!string.IsNullOrEmpty(e.Result))
    		{
    			//check result and set _isAppUpdateAvailable value
    		}
    	}
    	catch (Exception ex)
    	{
    		Debug.WriteLine(ex.StackTrace);
    	}
    	finally
    	{
    		(e.UserState as AutoResetEvent).Set();
    	}
    }
    
    	.
    	.
    	.

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: Making a synchronous web request from UI thread.

    The call to appUpdateAutoResetEvent.WaitOne() is blocking the UI thread.

    Have you tried something like this?

    Code:
    Constructor()
    {
    	//Show progress bar
    	InvokeCheckForAppUpdate();
    }
    
    public void InvokeCheckForAppUpdate()
    {
    	ServiceManager.ServiceManagerInstance.CheckForAppUpdate();
    }
    
    
    //ServiceManager class
    	.
    	.
    	.
    
    
    public void CheckForAppUpdate()
    {
    	WebClient webClient = new WebClient();
    	.
    	.
    	.
    	webClient.UploadStringAsync(uri, "POST", postData.ToString(), appUpdateAutoResetEvent);
    }
    
    private void appUdateWebClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
    	try
    	{
    		if (!string.IsNullOrEmpty(e.Result))
    		{
    			//check result and set _isAppUpdateAvailable value
    		}
    		if (_isAppUpdateAvailable)
    		{
    			// take required actions
    		}
    		//hide the progress bar
    	}
    	catch (Exception ex)
    	{
    		Debug.WriteLine(ex.StackTrace);
    	}
    }
    
    	.
    	.
    	.

  11. #11
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: Making a synchronous web request from UI thread.

    Hi Paulo,
    If i go with this approach then i will need to expose progress bar, and other UI controls which i want to enable disable depending on rest call response (since those wont be accessible in ServiceManager class). Which i think is not a good thing to do. Also i cant make network calls directly from my .xaml.cs file as all network calls in the app goes via ServiceManager class.
    Let me know if i am mistaken or you have some better approach.

    Regards,
    Vinay

  12. #12
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    542

    Re: Making a synchronous web request from UI thread.

    Also, make sure to test this on a device with the debugger *not* attached. If you delay a page from initialising for more than a few seconds, the system watchdog can kill you app as it assumes it's crashed/frozen.

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: Making a synchronous web request from UI thread.

    I misunderstood your code, sorry.

    But you'll just have to do the same thing with your ServiceManager class. That class would have a command method to kick off the call and an event to notify the answer. Pretty much encapsulating the call to WebClient.

    How familiar are you with C# 5.0's async/await? Are you willing to give it a try?

  14. #14
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: Making a synchronous web request from UI thread.

    Hi theothernet,
    Quote Originally Posted by theothernt View Post
    Also, make sure to test this on a device with the debugger *not* attached. If you delay a page from initialising for more than a few seconds, the system watchdog can kill you app as it assumes it's crashed/frozen.
    Good point. But if you look at the code, i am not delaying the initialization. After making the asynchronous call to server page initialization continues normally.

    Regards,
    Vinay

  15. #15
    Regular Contributor
    Join Date
    Dec 2012
    Posts
    66

    Re: Making a synchronous web request from UI thread.

    Quote Originally Posted by paulo.morgado View Post
    I misunderstood your code, sorry.

    But you'll just have to do the same thing with your ServiceManager class. That class would have a command method to kick off the call and an event to notify the answer. Pretty much encapsulating the call to WebClient.

    How familiar are you with C# 5.0's async/await? Are you willing to give it a try?
    Oh boy.! Yes i knew about async/wait. Even used it while creating a music app using Nokia Music API's. But somehow it slipped out of my mind. Will try it and update over results here. Thanks.

    Regards,
    Vinay

Similar Threads

  1. Replies: 3
    Last Post: 2009-08-20, 12:20
  2. how to reissue gprs request while making outgoing or incoming calls
    By ash_21 in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2009-05-12, 12:38
  3. Making Multiple Thread
    By jitendra_bhuyan in forum Symbian
    Replies: 0
    Last Post: 2007-11-16, 16:10
  4. Making a http request
    By vooi1966 in forum Symbian
    Replies: 0
    Last Post: 2004-09-23, 03:26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×