Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 可移植类库中的Thread.Sleep()_C#_Portable Class Library - Fatal编程技术网

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

C# 可移植类库中的Thread.Sleep(),c#,portable-class-library,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"&

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>

调试
任意CPU
{C46B138E-CC30-4397-B326-8DD019E3874B}
图书馆
性质
x0xtest.AVR
x0xtest.AVR
v4.0
简介3
512
{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
真的
满的
假的
bin\Debug\
调试;痕迹
促使
4.
pdbonly
真的
bin\Release\
痕迹
促使
4.
{F78547AC-1CA1-4ADB-9FA8-3E7DEB682240}
x0xtest.Comm

尝试等待超时。

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

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

这将产生大量的问题,比如“Eeek,现在我该做什么”之类的问题。这里可能的解决方法是烧掉一个虚拟System.Threading.ManualResetEvent对象。它有一个WaitOne(TimeSpan)方法

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

问:我想问一下 System.Linq.Expressions.Expression类。
答:它在Windows Phone/Xbox上不受支持,所以它只在您以Silverlight+.NET为目标时显示

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

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

不幸的是,这是对Portable Library项目表面区域的一个延迟更改,我们这样做是为了让Metro应用程序能够运行并被引用。Metro风格的应用程序、Visual Studio 11和Windows 8的一个新功能是消除应用程序创建和控制自己线程的需要(这很难做到)。相反,我们的想法是利用语言(即
async
/
await
)和框架特性(
Task
)来执行应该在后台发生的操作并与之同步


替换内容(例如,
ManualResetEvent
Task.Delay
)完全取决于您的场景和目标平台。你能解释一下你在做什么吗?

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

System.Threading.Tasks.Task.Delay(ms).Wait();
作为一个临时替代品

System.Threading.Thread.Sleep(ms);

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

编译器怎么说?一个简短但完整的示例,包括错误消息,可能会有所帮助。问题可能出在项目的设置/配置中,但您会对库的用户造成伤害。所以不要解决这个问题,但要摆脱睡眠()。在这里同意@Henkholtman(我想)。你想解决什么问题,使你认为
Thread.Sleep
是正确的解决方案?@JonSkeet“System.Threading.Thread”不包含“Sleep”的定义。“它也不是智能意义上的。C#代码是不相关的。项目和开发机器的设置/配置更难传达。我将用项目文件更新我的Q。基本上,它是一个以.NET4和Silverlight4为目标的PCL。我昨天通过链接创建并安装的便携库工具。VS 2010是包含所有Microsoft更新的SP1。@Damien_不相信我正在通过RS232与设备通信,我需要暂停。我试图找到.NET for Metro的参考,它看起来像Thread.Sleep()在那里,但可能文档站点被延迟了。明确地说,“类型的文档说明了Metro风格应用程序的.NET API中包含哪些成员。”我想Metro风格的正确方法是
wait TaskEx。延迟
是PCL中的异步内容吗?我会检查,但猜不会。哦,你的出色回答回避了一个问题,我如何配置我的PCL不以Metro为目标?大概VS11提供了麻粒性?我不知道这些东西,直到它在我手中。VS11的测试版将于本月到期。@AleksandrDubinsky
Wait
Sleep
完全不同-你的意思可能是
.Wait()
?哇,David Kean!我有很多问题和讨厌的事情要对你说!要回答这个问题,我正在做的是通过串行总线驱动一个开源硬件设备。首先是一个桌面应用程序,然后转移到Silverlight(使用P/Invoke或Java JNI…MS在Silverlight和Metro中取消了RS232支持真是太可惜了)。关于线程,我不使用它们,但async/Task不能替代它们。单线程!=多线程。佩奇仍然建议在Metro中使用Thread类来执行长时间运行的任务。至于PCL本身,我看到了许多毫无意义的不兼容。PCL应该更努力地做到广泛兼容。它应该更厚,并模拟95%解析但“在出生时分离”的一些功能。例如,PCL没有Thread.Sleep()或Task.Delay()。这纯粹是运气,哈哈