0

So if i have some boolean that keeps changing from false to true and from true to false how can i know if it stays false for a chosen number of seconds? and still not pausing my code just to wait for it to be false for some seconds... and each time it becomes true the the timer resets

EDIT: Thats all what i tried but as expected it pauses everything and keeps waiting for the chosen number of seconds

 static bool IsFalseFor(int MILseconds, bool Target)
    {
        while (Target == false)
        {
            for(int i = 0; i != 10; i++)
            {
                if(Target == true)
                {
                    return false;
                }
                Thread.Sleep(MILseconds/10);
            }
            return true;
        }
        return false;
    }
Emily
  • 142
  • 10
  • Welcome to Stack Overflow! Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – Thomas Ayoub Apr 28 '17 at 07:22
  • If it works like that... Change it – EpicKip Apr 28 '17 at 07:22
  • 2
    You can wrap the boolean in an object and add an event that is raised whenever the boolean value is set – Keith Mifsud Apr 28 '17 at 07:23
  • 1
    Your question seems like you have an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why do you want to set a bool and a timer to check this bool instead of starting and stopping the timer to check if it runs for the chosen number of seconds? – Mighty Badaboom Apr 28 '17 at 07:25
  • The code that you showed will never work because `bool target` can never change, since it is a parameter to a function. (Also note how it should be `bool target` instead of `bool Target`, we do not capitalize the first letter of local variables.) – Mike Nakis Apr 28 '17 at 07:43
  • I think you're on the wrong path, but there might be several ways to solve what you're trying to do (I agree with the XY problem) - Maybe look into [`Func`](http://stackoverflow.com/questions/3624731/what-is-func-how-and-when-is-it-used), [`AutoResetEvent`](https://msdn.microsoft.com/en-us/library/system.threading.autoresetevent(v=vs.110).aspx) or (depending on what you need really) [`async await`?](http://blog.stephencleary.com/2012/02/async-and-await.html) – default Apr 28 '17 at 07:58

1 Answers1

1

simplest implementation I can think of:

    public DateTime lastSetFalse { get; private set; }

    public TimeSpan lastStayedFalseFor { get; private set; }

    private bool _flag;
    public bool Flag
    {
        get
        {
            return _flag;
        }
        set
        {
            if (_flag && !value)
            {
                lastSetFalse = DateTime.Now;
            }
            if (!_flag && value && lastSetFalse != null)
            {
                lastStayedFalseFor = DateTime.Now - lastSetFalse;
                //raise an event if necessary
            }
            _flag = value;
        }
    }

Use Flag field to store the boolean value. Note lastStayedFalseFor only returns the duration after the variable changed from false to true, alternatively, if duration is needed when the variable is still false, add and use following instead of lastStayedFalseFor:

    public TimeSpan stayingOrStayedFalseFor
    { get { return _flag ? lastStayedFalseFor : DateTime.Now - lastSetFalse;  } }
user5226582
  • 1,946
  • 1
  • 22
  • 37
  • I agree with others that if you provide more details about your problem in general, there probably will be a more "elegant" solution. – user5226582 Apr 28 '17 at 09:37
  • My code: ThreadStart TrueFalseStart = new ThreadStart(TRUEFALSE); Thread TrueFalse = new Thread(TrueFalseStart); TrueFalse.Start(); while(true) { Flag = target; Console.WriteLine(lastStayedFalseFor.Seconds); } but it gives out wierd numbers like 55 while the thread changes the value every 10 seconds am i using it wrong? @user5226582 – Emily Apr 28 '17 at 09:43
  • Are you sure it changes `Flag` value every 10 seconds? If `Flag` is set to `false` several times in a row, it counts as if it stayed `false` this whole time and does not reset the timer. – user5226582 Apr 28 '17 at 09:55