Results 1 to 6 of 6

Thread: Two time thresholds are supposed to trigger two distinct actions without simultaneity

  1. #1

    Thread Starter
    New Member nikoloz's Avatar
    Join Date
    Nov 2020
    Location
    Georgia, Tbilisi
    Posts
    12

    Question Two time thresholds are supposed to trigger two distinct actions without simultaneity

    Dear gentlemen,

    I have been designing some app with VS2005 C#.
    I intend to compare two distinct time thresholds designated 'pulltime' and 'releasetime' with current time picked up from the device the app is deployed and run.
    Once the app is launched the user is intended to input two distinct time thresholds: 'pulltime' and 'releasetime' into two textboxes.
    Once upon certain button on the form is hit timer begins to compare recent time picked from the device every 1000 ms.
    When the 'pulltime' threshold is reached, certain coil should be pulled up, as timer ticks away the 2nd threshold 'releasetime' is approaching, thus when reached the same coil should be released.
    The app operates without throwing exceptions, first off when the 'pulltime' is reached it pulls coil, but when 'releasetime' is reached it initially releases coil but in a second pulls it up again.
    To give you an idea what I am trying to accomplish I add some code below:

    First off I input 'pulltime' and 'releasetime' in the two textboxes as the strings:

    string pulltime=TextBox1.Text; // input 'pulltime' as string
    DateTime pulltime1=DateTime.Parse(pulltime); //convert string into date time
    string releasetime=TextBox2.Text; //input 'releasetime' as string
    DateTime releasetime1=DateTime.Parse(releasetime); //convert string into date time

    The code below stands for pick up recent date time of the device on which app is run, extracts only time from DateTime
    object, converts it into the string to display on the form, and then converts it into Date Time object again:

    DateTime dt=DateTime.Now; //pick up recent date time of the board each 1000 ms
    string dt1=dt.TimeOfDay.ToString(); //extracts only time, converts into string to display on the form
    DateTime dt2=DateTime.Parse(dt1); //converts the string into date time object to compare with lower and time upper
    //thresholds

    the 3rd section of the code below is to compare recent time of the device with pulltime1 and releasetime1 thresholds

    Int32 result=DateTime.Compare(pulltime1, dt2); //before dt2 reaches pulltime1 the value is 1, from the moment it has
    //reached its value is -1
    Int32 result1=DateTime.Compare(releasetime1, dt2); //before dt2 reaches releasetime1 the value is 1, from the
    //moment it has reached its value is -1

    When code runs on the device first off the lower threshold is reached the coil pulls up, time runs to the upper threshold
    and upon it is reached the coil is released, then in one second is pulled up again and begins cycling around pulled and
    released actions, until I deliberately cease the application.

    Could you please take me into understanding, how this simultaneity of the actions could be avoided?
    How to enhance this code, to make it perform only once pull up coil action when the lower threshold is reached, and only once release coil action when the upper threshold is reached?

    Faithfully,
    N.Kv.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Two time thresholds are supposed to trigger two distinct actions without simultan

    You should keep track of whether the actions have been performed yet, using a boolean variable (eg: bool pullDone = false), and mark it as done when it is. When checking if the time has been reached, also check if the action has already been done - if it has, don't do it again.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Two time thresholds are supposed to trigger two distinct actions without simultan

    Why not use two separate Timers that are set to Tick and the precise time you want the two actions to be performed? The Timers won't be exact to the millisecond, so it depends how accurate you need to be. I'd also suggest using DateTimePicker controls rather than TextBoxes, assuming this is WinForms. There's no validation or conversion necessary then.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member nikoloz's Avatar
    Join Date
    Nov 2020
    Location
    Georgia, Tbilisi
    Posts
    12

    Re: Two time thresholds are supposed to trigger two distinct actions without simultan

    Thank you sir, I'll give it a good shot!

  5. #5

    Thread Starter
    New Member nikoloz's Avatar
    Join Date
    Nov 2020
    Location
    Georgia, Tbilisi
    Posts
    12

    Re: Two time thresholds are supposed to trigger two distinct actions without simultan

    Yes sir, the app uses two timers, when lower threshold is reached, the 1st timer disables itself and enables the second timer. When the upper threshold is reached, the 2nd timer disables itself and enables the 1st, this leads to the pull-release cycling. Because both conditions are fulfilled! It begins to pull up and release simultaneously! What I intend is the conditions to pull and release must mutually exclude each other, but now they are concurrent conditions, which contend to re-gain control over the thread!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Two time thresholds are supposed to trigger two distinct actions without simultan

    Quote Originally Posted by nikoloz View Post
    Yes sir, the app uses two timers, when lower threshold is reached, the 1st timer disables itself and enables the second timer. When the upper threshold is reached, the 2nd timer disables itself and enables the 1st
    If you're going to do it that way then it's pointless using two Timers. You may as well just use one and use a Boolean flag to indicate what gets done on the Tick. What I meant was one Timer per action, i.e. one to pull and one to release. They are both active all the time and they are set to Tick at the precise moment you want the action to occur. The first one Ticks at the pull time and pulls while the second Ticks at the release time and releases. No conflict.

    You could even use a single Timer for this and either an Enum or a Boolean to indicate the action to perform. You could set the Interval so it Ticks at the puul time and then pulls, then toggle to flag that indicates the action to perform and reset the Interval to Tick at the release time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width