从同一个类中引用类中的变量

时间:2018-07-10 06:39:43

标签: c# class variables static-libraries

我目前是第一次需要将我的应用程序重写到库中。到目前为止,我已经成功了,但是我需要以某种方式进行自动重复过程,这可以通过简单地使用camShield.start()来启动。

但是我无法从任何地方引用已启用。这里的想法是我将使用计时器启动线程,该计时器将检查启用的变量。但是要做到这一点,我需要另一个函数,例如stop(),它将启用的变量设置为false。

有没有更好的方法来实现这种功能?
---编辑----
我需要编写函数CamShield.start()和CamShield.stop(),以便能够访问CamShield.enabled变量。

这是代码的一部分,我正在尝试解决(这是类库)

using SharpAdbClient;
using System;
using System.Diagnostics;
using System.Threading;

namespace BaReader
{
    public class Private
    {
        public class CamShield
        {
            internal bool enabled = true;

            public static void start()
            {
                new Thread(() =>
                {
                    Thread.CurrentThread.IsBackground = true;                        
                    Timer camShieldTimer = new Timer(tap, null, 0, 20000);
                }).Start();
            }
        }

        internal static void tap(Object o)
        {
            AdbClient.Instance.ExecuteRemoteCommand("input tap 600 900", Device.lookup(), null);
            Debug.WriteLine("Tapped");
        }
    }
}

提前感谢任何想法。

2 个答案:

答案 0 :(得分:1)

您已将方法声明为静态,并且将变量启用为非静态,因此无法访问它,

    public class CamShield
    {
        internal bool enabled = false;
        public void start()
        {
            if(!enabled)
            {
                enabled = true;
                //your code to start
            }
        }

        public void stop()
        {
            if(enabled)
            {
                //your code to stop
                enabled = false;
            }
        }
    }

我确信您可以实例化CamShield类并从外部访问start和stop方法。

答案 1 :(得分:0)

为了停止线程,您需要使用Abort将其杀死。所附的问题将为您提供足够的工具和知识以达到目标。

秒,由于您的范围,您无法访问已启用。再看一下代码:

    public class Private
    {
        public class CamShield
        {
            internal bool enabled = true;

            public static void start()
            {
                new Thread(() =>
                {
                    Thread.CurrentThread.IsBackground = true;                        
                    Timer camShieldTimer = new Timer(tap, null, 0, 20000);
                }).Start();
            }
        }

        internal static void tap(Object o)
        {
            AdbClient.Instance.ExecuteRemoteCommand("input tap 600 900", Device.lookup(), null);
            Debug.WriteLine("Tapped");
        }
    }

您的internal bool enabled在CamShield类的范围内,除非您初始化CamShield类,否则您的tap方法将不可用。

要使用您的internal bool enabled,您需要在私有类中声明它,然后在tap中使用它:

    public class Private
    {
        internal bool enabled = true;

        public class CamShield
        {
            enabled = false;
            public static void start()
            {
                new Thread(() =>
                {
                    Thread.CurrentThread.IsBackground = true;                        
                    Timer camShieldTimer = new Timer(tap, null, 0, 20000);
                }).Start();
            }
        }

        internal static void tap(Object o)
        {
            enabled = true;
            AdbClient.Instance.ExecuteRemoteCommand("input tap 600 900", Device.lookup(), null);
            Debug.WriteLine("Tapped");
        }
    }