Pausing a thread in C#

In this article, I will explain how to pause a thread by using Thread.Sleep method in .NET Framework.
  • 7989

Introduction

Pausing a thread is an important concept in multithreading application. Sleep is found in Thread class. The Thread.Sleep method can be used to pause a thread for a fixed period of time. The Thread.Sleep method is receive a value in millisecond.

Syntax

Thread.Sleep(Time in millisecond);

Example

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

namespace multi

{

    class Program

    {

        static void Main(string[] args)

        {

            Thread th = new Thread(WriteY);

            th.Start();

            for (int i = 0; i <= 10; i++)

            {

                Console.WriteLine("Hello");

                Thread.Sleep(1000);

            }

        }

        private static void WriteY()

        {

            for (int i = 0; i <= 9; i++)

            {

                Console.WriteLine("world");

                Thread.Sleep(500);

            }

        }

    }

}

 

Output


Clipboard253.jpg

© 2020 DotNetHeaven. All rights reserved.