Bug 20578 - Thread.Sleep Never Finishes
Summary: Thread.Sleep Never Finishes
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.13.x
Hardware: PC Mac OS
: High normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-06-13 07:45 UTC by Alek Slater
Modified: 2017-06-27 17:31 UTC (History)
12 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_REPRODUCIBLE

Description Alek Slater 2014-06-13 07:45:21 UTC
Description of Problem:
Thread.Sleep never finishes. I.e. After the desired time has passed, the Thread does not resume.

Steps to reproduce the problem:
1. Try Sample project found here: http://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/
(But basically any Thread.Sleep in UI or in ThreadPool.QueueUserWorkItem seems to get stuck)


Actual Results:
It gets stuck at the sleep, never finishes, even after the desired sleep time.

Expected Results:
Should not get stuck, should keep going after the desired sleep time has passed.

How often does this happen? 
All the time.

Additional Information:
Tested it on Android x86 4.0.3.
Comment 1 Alek Slater 2014-06-13 08:07:43 UTC
I've tried the same sample code project on Android x86 4.1 and it worked OK, so seems to be version specific.
Comment 2 Alek Slater 2014-06-13 08:17:00 UTC
It is also broken on on Android x86 2.3.
Comment 3 Atin 2014-06-13 09:38:26 UTC
I have checked this issue and I am able to reproduce this issue .

Steps to reproduce this issue:
 
1. Download the sample application from “http://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/”
2. Open the sample application on XS.
3. Deploy the “SplashScreen” application on API 15 x86 emulator.
4. Application gets stuck at the Thread.Sleep(1000).

I observed that when I run the “SplashScreen” application on  “API 15 x86 emulator.” it gets stuck at the “Thread.Sleep(1000)". However, It is working on device(Samsung Note 3). I have also tried this issue with "API 16 emulator" and it is working fine for it.

Screencast: http://www.screencast.com/t/gjLUfq7ne

adb logs:https://gist.github.com/saurabh360/cd2e27ae51d71082e7f9
Application output : https://gist.github.com/saurabh360/fc2beb672167ae2509d6

Environment Info:

Mac OS X 10.9.2
Xamarin Studio: 5.0 (build 878)
Xamarin.Android: 4.12.4 

Build Information
Release ID: 500000878
Git revision: bcd66920d963483e7d638a2339c4022fe035b529
Build date: 2014-05-27 17:36:26-04
Xamarin addins: da9064ce55b0fa90930a7c437a4cc1ae0e5c778c
Comment 4 Matt Jones 2014-06-13 09:39:47 UTC
This is a showstopper for us and I suspect many others.
Comment 5 Matt Jones 2014-06-13 10:38:37 UTC
Worked on 4.04
Comment 6 Alek Slater 2014-06-17 04:58:29 UTC
What specifically would I have to install in terms of older versions so I could build without this issue?
Comment 7 Matt Jones 2014-06-17 10:54:59 UTC
We're starting to wonder if this is confined to x86 devices and emulators.
Comment 8 Kent Green [MSFT] 2014-06-17 13:40:09 UTC
I was able to duplicate the bug in API 15 using Thread.Sleep as in the example posted above. However; by using Task.Delay instead, the program works; and moreover, it appears that for most purposes Task.Delay is a prefered method for most purposes; since Thread.Sleep effectively pauses the program entirely, whereas Task.Delay only pauses the associated task as discussed here:

http://social.technet.microsoft.com/wiki/contents/articles/21177.visual-c-thread-sleep-vs-task-delay.aspx

Here are the changes that you would need to make in SplashActivity.cs to fix the bug in the "Creating a Splash Screen" sample:


namespace SplashScreen
{
>    using System.Threading; -> using System.Threading.Tasks

    using Android.App;
    using Android.OS;

    [Activity(Theme = "@style/Theme.Splash", MainLauncher = true, NoHistory = true)]
    public class SplashActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
>           Thread.Sleep(2500); -> Task.Delay(2500);
            StartActivity(typeof(Activity1));
        }
    }
}
Comment 9 Matt Jones 2014-06-18 07:42:37 UTC
Thanks a lot for taking a look at this so quickly, it's greatly
appreciated. For most purposes this workaround might be acceptable but
we have a lot of legacy code liberally peppered with Sleeps. So it's
no good for us I'm afraid. We really need Thread.Sleep to do exactly
what it used to before it was broken in the last release. It is
(unfortunately) a fundamental part of lots of .Net code out there.
Comment 10 Grigory (Playtika) 2014-06-23 12:06:24 UTC
Kent, u're completely wrong. 
Task.Delay creates a new task and doesn't block anything.
Comment 11 Kent Green [MSFT] 2014-06-23 12:43:47 UTC
My apologies if I misunderstood. Was the workaround at all helpful with your issue though?
Comment 12 Matt Jones 2014-06-24 04:18:14 UTC
No, it wasn't helpful. See previous comment of 18/06/14. Thanks for looking at it though.
Comment 13 Matt Jones 2014-06-24 10:48:10 UTC
I'm reluctant to release any new builds built with this version. Please get back to us soon about this issue.
Comment 14 dean.ellis 2014-07-30 11:14:30 UTC
I can't reproduce this on 4.12.6 Putting a thread sleep in the OnCreate as per Comment 8 does not lock up the application. This is running on a genymotion
Comment 15 Matt Jones 2014-07-30 11:20:52 UTC
Have you tried it on the regular emulator?
Comment 16 Alek Slater 2014-08-07 08:09:00 UTC
I can still reproduce this on both x86 images in Virtualbox and regular x86 emulators from Google. I have not tried genymotion.
Comment 17 Ankit Jain 2015-03-14 14:49:51 UTC
I am not able to reproduce this with the Splash screen sample project on:

OSX Yosemite 10.10.2
Xamarin Studio: Version 5.8 (build 443)
Xamarin.Android: Version: 4.20.0.37

on an Android API 15, 4.0.3, x86 emulator. The thread wakes up after the Thread.Sleep
and continues execution. I tried to run it in the debugger and without it. It worked in
both cases.

Alek, Could you please test this with the latest Xamarin Studio?
Comment 18 Chris Hardy [MSFT] 2017-06-27 17:31:36 UTC
Unfortunately, we’re unable to reproduce this report. If this issue is still occurring for you, please reopen this issue and attach a reproduction to the bug by starting with a clean Xamarin.Android project adding just the code necessary to demonstrate the issue.