Thread.Sleep (asynchronously) in a Windows Store App (WinRT)

UPDATE:  Another option is to use await Task.Delay(ms) which would require you to be in an async method (which my examples are).

I thought I had a simple idea that would require simple code: In my “service mock” class I wanted to sleep the thread for 2 seconds to simulate a real web service call to see how my UI reacted.

Simple, right? Just throw a good-ol’ Thread.Sleep() in there and all is well? Nope!

Issue #1 – System.Threading.Thread is no more …
Luckily, a quick Internet search (I refuse to turn the name of my search engine into a verb) revealed a “hack” to sleep on a thread:

new System.Threading.ManualResetEvent(false).WaitOne(ms);

Issue#2 – But it blocks my thread.
Ok … but this blocks my thread and C# 5 gives me a nice async/await pattern so as not to block my thread and MRE is not awaitable.  Ok … I’ll just spawn off a task and sleep on it.

await Task.Run(() => { new System.Threading.ManualResetEvent(false).WaitOne(2000); });

Or use Task.Delay inside an async lamba or method:

await Task.Delay(2000);

There you have it.  I can now simulate a slow web service call in my stub/mock service implemenation.

Post a comment if there is a better way to do it or just to contemplate the deep meaning of  “Sleep asynchronously.”

Advertisements

Upgrading Windows 7 Ultimate to Windows 8 Enterprise

After having such a great time at Build I felt it was time to upgrade my work laptop to Windows 8.  Previously, however, I had Windows 7 Ultimate and when I tried to upgrade to Windows 8 Enterprise the installer told me I could not do so: “Windows 7 Ultimate cannot be upgraded to Windows 8 Enterprise.”  The natural path is to upgrade to Windows 8 Pro but I specifically wanted Enterprise since that was what we have at work and for the ability to side-load Windows Store apps (though maybe Pro can do this as well).

Anyway, just by luck I happened upon a post about a very similar problem going from Vista to Windows 7 Enterprise … same issue.  The fix?  Totally stupid … really.

  1. Close the Windows 8 installer if you have it open and
  2. Launch RegEdit to edit the registry
  3. Find the following branch: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\Current Version
  4. Edit the following keys:
    1. EditionId – Change from “Ultimate” to “Enterprise”
    2. ProductName – Change from “Windows 7 Ultimate” to “Windows 7 Enterprise”

Now the in-place upgrade will run just fine.