2

I am currently for the first time in need to rewrite my app into library. I have succes so far, but I need to somehow make auto repeated process, which could be started by simply camShield.start().

But I am not able to reference the enabled from anywhere. The idea here is that I would start thread with timer, which would be checking on the enabled variable. But to do that, I need another function, like stop(), which would set the enabled variable to false.

Is there any better way to implement such a function?
---EDIT----
I need to write functions CamShield.start() and CamShield.stop(), which would be able to access the CamShield.enabled variable.

Here is part of code, where I am trying to solve it (It is Class Library)

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");
        }
    }
}

Thanks in advance for any ideas.

Tomáš Filip
  • 727
  • 3
  • 6
  • 23
  • Not able to understand what problem you are facing, can you please add more clarity, what you are trying to do, or what error you are facing? That will improve the chances of getting solution. – Mukesh Modhvadiya Jul 10 '18 at 06:55
  • `internal` means the variable is accessible from within the library. Make it public if you wish to access from outside the library. – PepitoSh Jul 10 '18 at 06:55
  • The catch is, that I dont know, how to write frunctions .start() and .stop() in the same class, because when I make those functions in either Private class, or CamShield, I cant access CamShield.enabled from CamShield.start(), nor CamShield.stop() – Tomáš Filip Jul 10 '18 at 07:00
  • Do not use static methods. – PepitoSh Jul 10 '18 at 07:01
  • How does that help me? Even if start() is not static, I cant acces the enabled variable from within it. – Tomáš Filip Jul 10 '18 at 07:11

2 Answers2

1

You have declared methods static and your variable enabled as non static so you were not able to access it,

    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;
            }
        }
    }

I am sure you can instantiate the CamShield class and access start and stop methods from outside.

Mukesh Modhvadiya
  • 2,178
  • 2
  • 27
  • 32
0

In order to stop the thread, you need to kill it using Abort. The attached question will provide you with enough tools and knowledge to get there.

second, you cannot access the enabled because of your scope. Take another look at the code:

    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");
        }
    }

Your internal bool enabled is in the scope of CamShield class and won't be available to your tap method unless you initialize CamShield Class.

in order to use your internal bool enabled you need to declare it in your private class and then use it in 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");
        }
    }
Barr J
  • 10,636
  • 1
  • 28
  • 46
  • Hello, thanks for the response. I dont need it to be avaible in tap() function, but anyway even if I declare it like you, right under Private, then I cant call it in CamShield, nor tap() either. – Tomáš Filip Jul 10 '18 at 07:05
  • try declaring it as public. – Barr J Jul 10 '18 at 07:13