Thread.Sleep() in .NET Core on Mac

5.2k views Asked by At

I'm working with .NET Core on my Mac machine.

Somewhere in my code I want to use this code:

System.Threading.Thread.Sleep(100);

But it can not find Thread in System.Threading namespace.

What's wrong? Isn't Thread available for Mac, or I'm missing something?

2

There are 2 answers

4
Ian Kemp On BEST ANSWER

Isn't Thread available for Mac?

Not yet: https://github.com/dotnet/corefx/issues/2576

The current System.Threading.Thread (and System.Threading.ThreadPool) packages only support desktop and CoreCLR.

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

I can see that the latest System.Threading.Thread package in the dotnet-core NuGet feed hasn't changed this picture.

0
Anton Sizikov On

Do you have System.Threading.Thread package added to your project.json?

{
    "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": { }
    }
}

Then I can use 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);
            }
        }
    }

enter image description here