22

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>
4

5 回答 5

25

这是“便携”的不幸副作用。库通过减法变得高度可移植,删除了许多可能目标之一上不可用的所有部分。这对 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) 方法。

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

问:我想问一下 System.Linq.Expressions.Expression 类的 Compile 方法是怎么回事。
答:Windows Phone/Xbox 不支持此功能,因此仅当您以 Silverlight + .NET 为目标时才会显示。

哎哟。便携,运动。这需要炖一段时间。我总体上对 DevDiv 尤其是 David Kean 表示同情,这是一项艰巨的工作。

于 2012-02-12T23:06:08.390 回答
19

(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?

于 2012-02-19T04:12:46.883 回答
18
System.Threading.Tasks.Task.Delay(ms).Wait();

作为替代品

System.Threading.Thread.Sleep(ms);

这在移植旧代码库时效果很好。

于 2015-07-24T13:10:25.453 回答
5

尝试在http://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx上等待超时。

于 2012-02-12T23:04:15.930 回答
4

你可以Task.DelaySystem.Threading.Tasks

于 2015-04-05T06:05:51.893 回答