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

Ale*_*sky 22 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>
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 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的问题,"Eeek,现在我该做什么".这里可能的解决方法是刻录一个虚拟的System.Threading.ManualResetEvent对象.它有一个WaitOne(TimeSpan)方法.

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

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

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


Dav*_*ean 19

(我'拥有'微软的便携式图书馆项目)

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

作为替代使用什么(例如ManualResetEvent,Task.Delay),完全取决于你的情况,你的目标是什么平台.你能解释一下你在做什么吗?

  • 感谢您的反馈.我们正在努力改进支持,我鼓励您下载Visual Studio 11 Beta并告诉我们您的想法.正如我在[频道9视频](http://channel9.msdn.com/Shows/Going+Deep/NET-45-David-Kean-and-Marcea-Trofin-Portable-Libraries)中所谈到的那样,这是非常困难的为已经完成和发货的平台提供理智的便携式故事.事实上的可移植性在所有情况下都不起作用,例如,直到我们在4.5中修复它,ObservableCollection并没有生活在所有平台上的相同位置. (3认同)
  • 哇,大卫基恩!我有很多问题和讨厌的事要对你说!要回答,我正在做的是通过串行驱动开源硬件设备.首先作为桌面应用程序,然后进入Silverlight(使用P/Invoke或Java JNI ...在MS上使用SHAME来杀死Silverlight和Metro中的RS232支持).关于线程的主题,我不使用它们,但async/Task不能替代它们.单线程!=多线程.[This](http://msdn.microsoft.com/en-us/library/windows/apps/br230302(v = vs.110).aspx #threading)页面仍然说使用Thread类来执行长时间运行的任务地铁. (2认同)
  • 我们对未来平台进行了更改,以便不再出现这些看似"随机"缺失的API.例如,如果您只选择".NET 4.5"和".NET for Metro样式应用程序",那么您将获得它们之间常见的_every single_ API,不幸的是,它几乎不可能为以前的版本执行此操作. (2认同)

And*_*ner 18

System.Threading.Tasks.Task.Delay(ms).Wait();
Run Code Online (Sandbox Code Playgroud)

作为替代品的替代品

System.Threading.Thread.Sleep(ms);
Run Code Online (Sandbox Code Playgroud)

这在移植遗留代码库时工作正常.