Thread.Sleep() in a Portable Class Library

前端 未结 5 2029
长发绾君心
长发绾君心 2021-02-18 15:05

The MSDN docs say Thread.Sleep() can be used in a portable class library. The compiler says otherwise. What are my alternatives besides a spin-loo

5条回答
  •  感动是毒
    2021-02-18 15:28

    This is the unfortunate side effect of "portable". A library becomes highly portable by subtraction, removing all the parts that are unavailable on just one of the many possible targets. That played havoc with the Thread class, it is quite devoid of any useful members. Just 5 of them left, MemoryBarrier(), CurrentCulture, CurrentThread, CurrentUICulture and ManagedThreadId.

    This might look odd, the intersection of the advertized targets certainly support more. This is probably related to the un-advertized one. The upcoming version of Windows 8 that will run on ARM cores. Otherwise known as WinRT or Metro or the ".NET for Metro style apps" API, depending on what tools you use. WinRT severely cuts down on the traditional Windows API, its System.Windows.Threading namespace is pretty empty.

    This is going to generated a ton of questions on SO, the "Eeek, now what do I do" kind. The possible workaround here is to burn up a dummy System.Threading.ManualResetEvent object. It has a WaitOne(TimeSpan) method.

    Fwiw, I'm personally not looking forward to programming against this library. By far the most alarming tidbit is in the Q&A section of the link you provided:

    Q: I wanted to ask what's up with the Compile method of the System.Linq.Expressions.Expression class.
    A: It's not supported on Windows Phone/Xbox, so it only shows up when you target Silverlight + .NET.

    Ouch. Portable, sportable. This needs to stew for a while. My sympathies to DevDiv in general and David Kean in particular, tough job.

提交回复
热议问题