Thread.Sleep() in a Portable Class Library

前端 未结 5 2027
长发绾君心
长发绾君心 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.

    0 讨论(0)
  • 2021-02-18 15:30

    Try waiting on a http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx with a timeout.

    0 讨论(0)
  • 2021-02-18 15:37
    System.Threading.Tasks.Task.Delay(ms).Wait();
    

    works as a drop-in replacement for

    System.Threading.Thread.Sleep(ms);
    

    This works fine when porting a legacy code base.

    0 讨论(0)
  • 2021-02-18 15:37

    You can use Task.Delay in System.Threading.Tasks

    0 讨论(0)
  • 2021-02-18 15:44

    (I 'own' the portable library project at Microsoft)

    Unfortunately, this was a late change to the Portable Library project surface area that we made so that we could run and be referenced by Metro apps. One of the new things with Metro style apps, Visual Studio 11, and Windows 8 is to remove the need for apps to create and control their own threads (which is tough to get right). Instead, the idea is that you make use of language (ie async/await) and framework features (Task) to perform and synchronize with operations that should occur in the background.

    What to use as a replacement (for example, ManualResetEvent, Task.Delay) , entirely depends on your scenario and what platforms you are targeting. Can you explain what you are doing?

    0 讨论(0)
提交回复
热议问题