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

发布于 2025-01-05 04:34:10 字数 3612 浏览 1 评论 0原文

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>

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-loop? Thread.CurrentThread.Join() doesn't exist either.

Project file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只怪假的太真实 2025-01-12 04:34:10

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

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

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

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

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

哎哟。便携、运动。这个需要炖一会儿。我对 DevDiv 表示同情,特别是 David Kean,这是一项艰巨的工作。

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.

忆悲凉 2025-01-12 04:34:10

(我“拥有”Microsoft 的便携式库项目)

不幸的是,这是我们对便携式库项目表面区域进行的后期更改,以便我们可以运行 Metro 应用程序并被 Metro 应用程序引用。 Metro 风格应用程序、Visual Studio 11 和 Windows 8 的新功能之一是消除应用程序创建和控制自己的线程的需要(这很难做到正确)。相反,其想法是利用语言(即 async/await)和框架功能(Task)来执行操作并与操作同步这应该在后台发生。

使用什么来替代(例如,ManualResetEventTask.Delay)完全取决于您的场景以及您的目标平台。你能解释一下你在做什么吗?

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

夜光 2025-01-12 04:34:10
System.Threading.Tasks.Task.Delay(ms).Wait();

直接替代品。

System.Threading.Thread.Sleep(ms);

作为移植遗留代码库时的

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.

南街九尾狐 2025-01-12 04:34:10

您可以在System.Threading.Tasks中使用Task.Delay

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文