如何控制另一个类中的变量

本文通过C#编程实例,深入探讨了线程控制与锁机制的应用,特别是如何解决变量在多线程环境下的非规则输出问题,以及使用锁来确保数据一致性。
// 这个是 一次次执行,只是达到了控制另一个类中的变量的目的,并没有达到想要测试是否由于执行时间慢,还没有执行到函数(需要依据这个变量(isCallBox) 做分类执行的目的)这个变量就又改变了,现在用定时函数测试,然后观察----------如果会改变,需要考虑用锁,是否会达到不改变的目的
namespace ConsoleApplication11
{
    class Program
    {

        static int T { get; set; } = 1;
      //  static bool isCallBox = false;

        static void Main(string[] args)
        {
            T = 60;

            var I = (char)T;


            for(int i=0;i<100;i++)
            {
                if(i%2==0)
                {
                    Test.isCallBox = false;
                }
                else
                {
                    Test.isCallBox = true;
                }

                Test.PrintText();
               // System.Threading.Thread.Sleep(500);

            }

            //Class1 c1 = new Class1();
            //AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Class1, Class2DTO>().ForMember(d => d.ie, opt => opt.MapFrom(i => i.i)));


            //var c2=AutoMapper.Mapper.Map<Class2DTO>(c1);

            Console.WriteLine(I);

            Console.Read();
        }

     
    }

    public class Test
    {
        public static bool isCallBox=false;

        public static void PrintText()
        {
          
            //isCallBox = false;
            // System.Threading.Thread.Sleep(500);
            if (isCallBox)
            {
                Console.WriteLine("is true;");
            }
            else
            {
                Console.WriteLine("is false");
            }
        }
    }
}

//output 
  is false;
  is true;
  is false
  is true;
...... <

// 这样就是不规则输出

namespace ConsoleApplication11
{
    class Program
    {
        static private  Timer _jobTimer;
        static int i = 0;

        static int T { get; set; } = 1;
      //  static bool isCallBox = false;

        static void Main(string[] args)
        {
            T = 60;

            var I = (char)T;

            //for(int i=0;i<100;i++)
            //{
            //    if(i%2==0)
            //    {
            //        Test.isCallBox = false;
            //    }
            //    else
            //    {
            //        Test.isCallBox = true;
            //    }

            //    Test.PrintText();
            //   // System.Threading.Thread.Sleep(500);

            //}

            //Class1 c1 = new Class1();
            //AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Class1, Class2DTO>().ForMember(d => d.ie, opt => opt.MapFrom(i => i.i)));


            //var c2=AutoMapper.Mapper.Map<Class2DTO>(c1);
            StartJob();
            Console.WriteLine(I);

            Console.Read();
        }

        public static void StartJob()
        {
            _jobTimer = new System.Timers.Timer();
            _jobTimer.Interval = 500;
            _jobTimer.Elapsed += new ElapsedEventHandler(Timer_ChangeBoolValue);
            _jobTimer.Start();
        }

        private static void Timer_ChangeBoolValue(object sender, ElapsedEventArgs e)
        {
            if (i % 2 == 0)
            {
                Test.isCallBox = false;
            }
            else
            {
                Test.isCallBox = true;
            }
            i++;
            Test.PrintText();
        }
    }

    public class Test
    {
        public static bool isCallBox=false;

        public static void PrintText()
        {
          
            //isCallBox = false;
            System.Threading.Thread.Sleep(6500);
            if (isCallBox)
            {
                Console.WriteLine("is true;");
            }
            else
            {
                Console.WriteLine("is false");
            }
        }
    }
}

// output

is false 
is false
is true
is false
is true ...   非规则输出  , 加lock看看是否会杜绝非规则输出

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值