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

版权声明:转载请注明转载地址,谢谢! https://blog.csdn.net/softuse/article/details/83211363
// 这个是 一次次执行,只是达到了控制另一个类中的变量的目的,并没有达到想要测试是否由于执行时间慢,还没有执行到函数(需要依据这个变量(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看看是否会杜绝非规则输出

猜你喜欢

转载自blog.csdn.net/softuse/article/details/83211363