c# - 可移植类库中的 Thread.Sleep()

标签 c# portable-class-library

MSDN 文档说 Thread.Sleep() 可以在可移植 类库中使用。编译器另有说明。除了自旋循环,我还有哪些选择? Thread.CurrentThread.Join() 也不存在。

项目文件:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{C46B138E-CC30-4397-B326-8DD019E3874B}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>x0xtest.AVR</RootNamespace>
    <AssemblyName>x0xtest.AVR</AssemblyName>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <TargetFrameworkProfile>Profile3</TargetFrameworkProfile>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Attributes\AddressAttribute.cs" />
    <Compile Include="Attributes\RegAttribute.cs" />
    <Compile Include="Attributes\ROAttribute.cs" />
    <Compile Include="Attributes\RWAttribute.cs" />
    <Compile Include="Attributes\WOAttribute.cs" />
    <Compile Include="Devices\ATMega162.cs" />
    <Compile Include="Exceptions.cs" />
    <Compile Include="IntelHexFormat.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Proxy.cs" />
    <Compile Include="ProxyBase.cs" />
    <Compile Include="ProxyBase_UploadFirmware.cs" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\x0xtest.Comm\x0xtest.Comm.csproj">
      <Project>{F78547AC-1CA1-4ADB-9FA8-3E7DEB682240}</Project>
      <Name>x0xtest.Comm</Name>
    </ProjectReference>
  </ItemGroup>
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

最佳答案

这是“便携”的不幸副作用。一个库通过减法变得高度可移植,删除所有在许多可能目标中的一个上不可用的部分。这对 Thread 类造成了严重破坏,它完全没有任何有用的成员。只剩下 5 个,MemoryBarrier()、CurrentCulture、CurrentThread、CurrentUICulture 和 ManagedThreadId。

这可能看起来很奇怪,广告目标的交集肯定支持更多。这可能与未宣传的有关。即将在 ARM 内核上运行的 Windows 8 版本。也称为 WinRT 或 Metro 或“.NET for Metro 风格应用程序”API,具体取决于您使用的工具。 WinRT 严重削减了传统的 Windows API,其 System.Windows.Threading 命名空间非常空。

这将产生大量关于 SO 的问题,例如“Eeek,现在我该怎么做”之类的问题。此处可能的解决方法是燃烧一个虚拟的 System.Threading.ManualResetEvent 对象。它有一个 WaitOne(TimeSpan) 方法。

首先,我个人并不期待针对这个库进行编程。到目前为止,最令人震惊的花絮是在您提供的链接的问答部分:

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.

哎呀。便携的,运动的。这需要炖一会儿。我总体上对 DevDiv 表示同情,特别是对 David Kean,工作艰巨。

关于c# - 可移植类库中的 Thread.Sleep(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9251917/

相关文章:

c# - 使用 C# 将 Blob 文件从 Azure 下载到内存流

c# - 可移植类库中的 ConcurrentBag 替代方案

c# - 任务似乎互相阻塞

C# 屏幕截图 winlogon 以及用户桌面

c# - 如何 "restore"Wpf RichTextBox 中的插入符位置?

c# - 使用 xamarin.forms 跨平台 C# 在设备日历上设置提醒

wpf - 如何处理面向 Windows 应用商店应用程序和 WP7、WP8、WPF 的可移植类库中的图像?

c# - 可移植类库中的 MvvmLight - 如何进行单元测试

C# 可移植类库和具有嵌套属性的反射

c# - SELECT 从程序超时但在 SQL Server Management Studio 中工作