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

时间:2012-02-12 19:26:07

标签: 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>

5 个答案:

答案 0 :(得分:25)

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

这可能看起来很奇怪,广告目标的交集当然支持更多。这可能与未广告的相关。即将推出的将在ARM内核上运行的Windows 8版本。另外称为WinRT或Metro或“.NET for Metro style apps”API,具体取决于您使用的工具。 WinRT严重削减了传统的Windows API,其System.Windows.Threading命名空间非常空。

这将产生关于SO的问题 ton ,“Eeek,现在我该做什么”。这里可能的解决方法是刻录一个虚拟的System.Threading.ManualResetEvent对象。它有一个WaitOne(TimeSpan)方法。

Fwiw,我个人并不期待对这个图书馆进行编程。到目前为止,最惊人的消息是在您提供的链接的Q&amp; A部分:

  问:我想问一下编译方法有什么用   System.Linq.Expressions.Expression类。
  答:Windows Phone / Xbox不支持,因此只有在您定位Silverlight + .NET时才会显示。

哎哟。便携,可运动。这需要炖一段时间。我对DevDiv,特别是David Kean的同情,特别是艰难的工作。

答案 1 :(得分:19)

(我自己'是微软的便携式图书馆项目)

不幸的是,这是对我们制作的可移植库项目表面区域的最新更改,以便我们可以运行并由Metro应用程序引用。 Metro风格应用程序,Visual Studio 11和Windows 8的新功能之一是消除了创建和控制自己的线程的应用程序的需求(这很难做到正确)。相反,我们的想法是您使用语言(即async / await)和框架功能(Task)来执行应该在后台运行的操作。

用作替代品的内容(例如,ManualResetEventTask.Delay)完全取决于您的方案以及您定位的平台。你能解释一下你在做什么吗?

答案 2 :(得分:18)

System.Threading.Tasks.Task.Delay(ms).Wait();

的直接替代品
System.Threading.Thread.Sleep(ms);

这在移植旧代码库时工作正常。

答案 3 :(得分:5)

答案 4 :(得分:4)

您可以在Task.Delay

中使用System.Threading.Tasks
相关问题