Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.Sleep() in .NET Core on Mac

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?

like image 734
mehrandvd Avatar asked Apr 12 '16 09:04

mehrandvd


People also ask

What's thread sleep () in threading in C#?

Suspends the current thread for the specified number of milliseconds.

Does .NET core run on Mac?

NET Core is that you can run it on multiple platforms and architectures. So you can build an app that will run on Windows, but also on Linux, macOS and on different architectures like x86 and ARM. This is perfect for lots of scenarios, including desktop applications. You can learn about other reasons for using .

How do you call a sleep method in C#?

In c#, the sleep method is useful to suspend or pause the current thread execution for a specified time. We can suspend the thread execution either by passing the time in milliseconds or with TimeSpan property like as shown below. TimeSpan ts = new TimeSpan(0,0,1);

Does thread sleep block?

Sleep method. Calling the Thread. Sleep method causes the current thread to immediately block for the number of milliseconds or the time interval you pass to the method, and yields the remainder of its time slice to another thread. Once that interval elapses, the sleeping thread resumes execution.


1 Answers

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

like image 52
Anton Sizikov Avatar answered Oct 05 '22 03:10

Anton Sizikov