C#加锁,线程

下边是线程,lock之间的用法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsoleApp27
{
    class Program
    {
        static bool isRunning = true;
        static Object obj = new object();

        static void Main(string[] args)
        {
            Thread t = new Thread(NewThreadLogic);
            t.Start();
            t.IsBackground = true;
          

            //Console.ReadKey();

          //  isRunning = true;

            //Console.ReadKey();

            try
            {
                //t.Abort();

                //t = null;
            }
            catch
            {

            }
            while(true)
            {
                lock (obj)
                {
                    Console.SetCursorPosition(0, 0);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("@");
                }
            }
           // Thread.Sleep(1000);
        }

        static void NewThreadLogic()
        {
            while (isRunning)
            {
                lock (obj)
                {
                    // Thread.Sleep(1000);
                    //  Console.WriteLine("开启新的线程");
                    Console.SetCursorPosition(10, 5);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.Write("?");
                }
            }
        }
    }
}

版权声明:本文为charlsdm原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。