【问题标题】:Thread.Sleep() in .NET Core on MacMac 上 .NET Core 中的 Thread.Sleep()
【发布时间】:2016-04-12 09:36:27
【问题描述】:

我正在我的 Mac 机器上使用 .NET Core。

我想在我的代码中的某个地方使用此代码:

System.Threading.Thread.Sleep(100);

但是在System.Threading命名空间中找不到Thread

怎么了? Thread 不适用于 Mac,还是我遗漏了什么?

【问题讨论】:

  • 唔,别为它屏住呼吸。这在 CoreFx 启动之前很久就被删除了。 Task.Delay() 是前进的方向,它是正确的方式™

标签: c# macos .net-core .net-mac


【解决方案1】:

您的project.json 中添加了System.Threading.Thread 包吗?

{
    "version": "1.0.0-*",
    "compilationOptions": {
        "emitEntryPoint": true
    },

    "dependencies": {
        "NETStandard.Library": "1.0.0-rc2-23811",
        "System.Threading.Thread": "4.0.0-beta-23516"
    },

    "frameworks": {
        "dnxcore50": { }
    }
}

那我可以用Thread.Sleep:

using System;
using System.Threading;


    namespace ConsoleApplication
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Console.WriteLine(DateTime.Now);
                Thread.Sleep(2000);
                Console.WriteLine("Hello World!");
                Console.WriteLine(DateTime.Now);
            }
        }
    }

【讨论】:

    【解决方案2】:

    Thread 不适用于 Mac 吗?

    还没有:https://github.com/dotnet/corefx/issues/2576

    当前的 System.Threading.Thread(和 System.Threading.ThreadPool)包仅支持桌面和 CoreCLR。

    和:https://github.com/dotnet/corefx/issues/2576#issuecomment-187184341

    我可以看到 dotnet-core NuGet 提要中最新的 System.Threading.Thread 包并没有改变这张图片。

    【讨论】:

    • 阅读 issue2576 它将在 rc2 中可用,预计 rc2 将很快发货
    • @Eldho 因此我说“还没有”:)
    • 是的,只是确认一下
    • 但是这个问题是关于 UWP,而不是关于 OSX。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 2019-06-05
    相关资源
    最近更新 更多