Results 1 to 25 of 25

Thread: [RESOLVED] Weird Timer Behavior Issue

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Resolved [RESOLVED] Weird Timer Behavior Issue

    Its been a while, but I have a weird timer issue.
    I am fairly good using timers. I mostly use then for interface functions.

    But, I have an unbound datagrid (TXT file based data)
    It only holds 5-20 rows of items.

    It is a scheduler program that simply looks at the "Done" checkbox (if unchecked), reminder date and reminder time.
    If the criteria match the Now status. It should display a notice/ or dialog

    My issue is, that It works perfectly when the app starts.
    And it works perfectly when I manually click" my refresh button to execute the function to check the date/time.

    BUT, If use a timer. It will keep displaying the notice/dialog even If it was checked and I unchecked it.
    Its kind of feels like the timer isn't letting go of the variables or something.

    The only way the timer does work is if I manually click unto another row and reselect a different row and check/uncheck the "Done" box (column)

    I have tried refreshing the datagrid, sendkeys, disabling and enabling the timer. But nothing seems to check it every minute unless I manually reselect two rows.

    Has anybody run into this before??

    Name:  datagridtimer1.jpg
Views: 375
Size:  49.1 KB
    Last edited by pixelink; May 14th, 2022 at 04:30 PM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Weird Timer Behavior Issue

    No, and you have the wrong diagnosis, but we'd have to see some code to be able to figure it out.

    The timer doesn't 'let go of variables'. It's a simple beast. When the timer interval elapses, a tick event is posted to the message queue. If the UI is busy, that tick event may not be handled for some time. If the UI is not busy, the tick event will be handled right away.

    My first question was whether you had the interval too low, but you mentioned doing this once a minute, so that would mean that your interval is around 60,000. Below 100 gets a bit dicey, and 60,000 is not below 100.

    If I were to guess, I'd say that something is wrong with how you implemented this logic:
    If the criteria match the Now status.
    That's an easy one to get wrong. Still, without seeing code, there isn't anything more to say.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Weird Timer Behavior Issue

    Well, if you want to see the code. Here it is.

    But, like I said the timer works perfectly as long I manually select two different rows while the "Done check box is unchecked.
    I only have to do this once and just watch the timer work.

    But, if I don't manually select a couple of rows. It doesn't work at all when I uncheck a checkbox.

    Of course, if a "Done" checkbox is unchecked at startup it works fine. Only when I edit items and check/uncheck do I run into an issue.

    Its like it ignores the check boxes after it encounters a cycle through the function.

    It should always be notifying me every minute even If I check/uncheck the Done checkbox.
    I need to monitor the "Reminder Time" all the time. That is my goal.

    Code:
    Public Sub getSCHEDGlobalChk()
    
            Dim gRemDate As Date
            Dim R As Integer = 0
            Dim gTxt As String = ""
            Dim dtNow As DateTime = Now
            For R = 0 To dgScheduler.Rows.Count - 2
    
                'if reminder date is today then
                ' if reminder time = now then
                ' display notice/dialog
    
                gRemDate = CDate(dgScheduler.Rows(R).Cells(2).Value)
                gTxt = dgScheduler.Rows(R).Cells(0).Value
    
                'see if Done box is checked first
                If dgScheduler.Rows(R).Cells(4).Value = False Then
    
                    'check if reminder dt is now or before today (already past)
                    If gRemDate <= dtNow Then
                        'now check if time is now
    
                        gRemDate = CDate(dgScheduler.Rows(R).Cells(1).Value)
    
                        'display notices/dialog
                        If gRemDate <= dtNow Then
    
                           dgScheduler.ClearSelection()
                            dgScheduler.Rows(R).Selected = True
                            SendKeys.Send("{Down}")
                            Threading.Thread.Sleep(1000)
                            SendKeys.Send("{UP}")
                            sRow = R
    
                            lbSCHEDNotice.BackColor = Color.Red
                            lbSCHEDNotice.ForeColor = Color.Yellow
                            lbSCHEDNotice.Text = gTxt
                            'lbSCHEDNotice.Text = "A SCHEDULED EVENT IS DUE"
                            If chkSHEDMsg.Checked = True Then
                                'MessageBox.Show("A SCHEDULED EVENT IS DUE!!!", "Scheduler Notice", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                MessageBox.Show(gTxt, "Scheduler Notice", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            End If
    
                        End If
                    Else
                        'defMTNotice()
                    End If
                End If
            Next
            isStartupSCHED = False
            gRemDate = Nothing
            Timer3.Enabled = True
    
    End Sub
    Last edited by pixelink; May 14th, 2022 at 04:24 PM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Weird Timer Behavior Issue

    Also, just an FYI.
    I have been running this for a year (without the Reminder Time) and it has worked perfectly.
    It was only running at startup.

    But today, I added the Reminder Time and added the timer.
    I works great If I manually execute the function several times, even unchecking the done box.

    It just the timer that doesn't play nice.

    Also, I checked if the timer was firing regularly like it should and it does.
    Last edited by pixelink; May 14th, 2022 at 04:31 PM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Weird Timer Behavior Issue

    Just so you know how I test this at runtime.

    - Start app.
    - The first (in screenshot above) row has the "Done" checkbox unchecked
    - It notifies me at startup fine (that's the big RED square you see).
    - Now, if I CHECK the "Done" checkbox.... the timer keeps notifying me when it shouldn't.
    - To stop it, I then select a two other rows.
    - Then it will stop notifying me like its supposed to.
    - Then If I uncheck it again, it WILL NOT notify me UNLESS...
    - I select two rows. Then it starts notifying me again.


    - Same thing happens If I start the app with the "Done" checkbox UNCHECKED.
    - It doesn't notify me like it should
    - But, if I uncheck it, it doesn't work unless I reselect two other rows.

    FYI, the timer only fires the call to the function. No other code in the timer.
    Last edited by pixelink; May 14th, 2022 at 04:46 PM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Weird Timer Behavior Issue

    Well, after playing with it at runtime.
    I don't have to select two different rows, just one for it to work.

    So, it looks like to me the datagrid loses focus when I a check/uncheck the checkbox.


    So, back to the drawing board on how I can make the datagrid have a click event that works.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Weird Timer Behavior Issue

    The timer is too simple an animal to take any blame for this. The reason will end up lying elsewhere.

    So, what happened when you put a breakpoint in the timer tick event and stepped through that code? What path does it take when it is doing the right thing? How is that different from the path it takes when it does the wrong thing.

    The code you showed isn't the right code for figuring out this problem. You might find the problem in there once you step into it, though it doesn't look likely. The issue will be in the timer tick, or some event around the checkboxes, or something like that. It's more likely an issue with how you get to that method, rather than that method itself.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Weird Timer Behavior Issue

    Like I said. There is nothing to the timer.

    Code:
    Timer3.Enabled = False
    getSCHEDGlobalChk()  <---- this goes to the function above

    Get back to ya on the breakpoints.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Weird Timer Behavior Issue

    Okay... Either way, the timer runs but doesn't work unless you reset focus on the datagrid by clikcing on it.

    When its uncheck won't work, when its checked won't work.
    UNLESS you click on the datagrid.

    In debug/breakpoint. When it doesn't work it is skipping the unchecked checkbox as though it is still checked.
    but when it works it sees the checkbox correctly after i HAVE CLICKED ON THE DATAGRID, after the first startup

    That what I having been saying.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  10. #10
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Weird Timer Behavior Issue

    What I do if/when I encounter this type of thing is I create a brand new routine that is stripped of everything except what seems to not be working right, then slowly re-introduce the intended logic piece by piece.

    So, in this case, have the timer call a brand new routine, and in that routine, just loop through the datagridview and have it report the status of each checkbox in the Done column and see what the result is.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Weird Timer Behavior Issue

    Well, I tied to put just this code in the timer.
    Same thing. Won't work unless you click on the grid.
    And this is just checking if a checkbox is checked or not.

    Code:
    Dim R As Integer = 0
    For R = 0 To dgScheduler.Rows.Count - 2
                
                If dgScheduler.Rows(R).Cells(4).Value = False Then
                    Label1.Text = Label1.Text + "yes - "
                Else
             	Label1.Text = Label1.Text + "no - "
                End If
             
      Next
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  12. #12
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Weird Timer Behavior Issue

    The following link references C# but seems like it might be helpful.

    https://stackoverflow.com/questions/...w-not-updating

    Edit: and this:

    https://stackoverflow.com/questions/...er-checking-it

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Weird Timer Behavior Issue

    Okay... completely string the app down to just a form, the timer and the datagrid. NOTHING else is on the app.

    Same thing.

    Either this is a grid focus issue or the timer is malfunctioning somehow.

    Night-Night
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  14. #14
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Weird Timer Behavior Issue

    Bottom line from those posts, when you check/uncheck the box, the change is only made visually until the checkbox loses focus somehow, at which point the state it is in visually gets applied.

    It makes sense, because I believe the same is true for a text field in a grid. If you start making a change to the text field, your change doesn't actually take effect until you move focus out of that text field.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: Weird Timer Behavior Issue

    Wow... this worked...

    dgScheduler.CurrentCell = Nothing

    I put in my timer to reset the cell.

    I guess the issue is. When you uncheck a checkbox, its actually not really Null (false).

    Makes sense. That is why the timer kept seeing the box as True even though it was unchecked.
    Is this a bug or poor coding by the devs?


    I knew it had to be something like this.

    Thanks for pointing out this post.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Weird Timer Behavior Issue

    Back in VB6, the flex grid couldn't be edited. You could make it editable pretty easily by floating a textbox over the cell, sizing it to the same size as the cell, and moving the text from the cell into the textbox. After any changes, you moved the text back from the textbox to the cell. It looked like you were editing in the cell, but you were actually editing a textbox that was on top of the cell.

    I keep forgetting about it, but I believe the DGV works something like this, as well.
    My usual boring signature: Nothing

  17. #17
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] Weird Timer Behavior Issue

    I think it may also come down to the fact that you have a validate event that has to occur before the value is changed, so to support that, the change can't officially take place, i.e. be a permanent readable property until the edit is officially done. You were technically still in the edit phase of the control.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: [RESOLVED] Weird Timer Behavior Issue

    Its a check box, Not an editable field. Once you check/uncheck it, its done. There is no more to do.
    The issue is fixed (see above). It may not be completing the check (click) but that is because it is an issue with the datagrid itself. Nothing on my end can be done once you check or uncheck the box. There is no validating the checkbox outside of just clicking on it, unless you reselect a different row, which is probably validating. But, I should not have to click on another row for a timer to check it. That was the point of the issue. Not having to manually select another row to get the timer to see it.

    That's why this works... dgScheduler.CurrentCell = Nothing
    It validates it for me without having to click on another row.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  19. #19
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: [RESOLVED] Weird Timer Behavior Issue

    Quote Originally Posted by pixelink View Post
    Its a check box, Not an editable field. Once you check/uncheck it, its done. There is no more to do.
    That statement contradicts itself.

    Quote Originally Posted by pixelink View Post
    It may not be completing the check (click) but that is because it is an issue with the datagrid itself. Nothing on my end can be done once you check or uncheck the box. There is no validating the checkbox outside of just clicking on it, unless you reselect a different row, which is probably validating. But, I should not have to click on another row for a timer to check it. That was the point of the issue. Not having to manually select another row to get the timer to see it.
    You are doing this wrong. You are already neck deep in this and seem like a tete dur so I doubt you'll take this to the bank, but you should read what I have to say nonetheless.

    You should not try to manipulate data in your own application by doing things like SendKeys. You should not even be getting data directly from the DataGridView in this case. What you should have done is created some sort of data source (e.g. DataTable), bind the DataSource to the DataGridView, and then get/set the values on the underlying data source.

    Here is good information on ADO.NET data containers in general: https://www.vbforums.com/showthread....An-Explanation

    And here is good information on databinding to a DataGridView: https://docs.microsoft.com/en-us/dot...idview-control

    Quote Originally Posted by pixelink View Post
    That's why this works... dgScheduler.CurrentCell = Nothing
    It validates it for me without having to click on another row.
    Which is completely unnecessary if you would manipulate an underlying data source rather than the UI control that displays the data.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Weird Timer Behavior Issue

    Yeah, it's a checkbox, but it all works using the same model. I don't know that a checkbox is not implemented as a checkbox control that floats over the underlying cell. The underlying cell is not a checkbox, nor can it be, since the DGV is a representation of a backing object (usually a datatable, though not always).

    Shifting the current cell is sufficient, apparently, so that's good enough.
    My usual boring signature: Nothing

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Apr 2019
    Location
    Sabattus
    Posts
    234

    Re: [RESOLVED] Weird Timer Behavior Issue

    What I meant by "editable field" was that its not a text field. Its just a check box. Once you click on it, its done.
    Since this is NOT a DB driven data source, I don't have to validate or commit anything to the datagrid in code.

    ITS FIXED... why are we still going on about this.
    Last edited by pixelink; May 17th, 2022 at 10:42 AM.
    Can't Type - Forgetful - Had Stroke = Forgive this old man!
    Website: https://pixelinkmedia.wixsite.com/frankhilton
    VSCOMM 2022 • LAZARUS 2.2.0 • Win10 • 16G RAM • Nvida GForce RTX 2060

  22. #22
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: [RESOLVED] Weird Timer Behavior Issue

    Well, part of it is that this thread will be preserved on this site and will likely be referenced in the future by others who are facing the same problem, so a discussion of WHY it is happening is always valuable in case someone else would like to take a different approach to working around the issue.

    Reducing the issue to "DataGridView is broken, do this line of code" is not extremely helpful and not really an accurate representation of what is going on behind the scenes.

  23. #23
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: [RESOLVED] Weird Timer Behavior Issue

    Quote Originally Posted by pixelink View Post
    ITS FIXED... why are we still going on about this.
    It is not fixed, you simply found a hack workaround your bad architecture design decision.

    To expand what OptionBase1 said, since this thread will live on with or without you, I wanted to make it obvious to others who visit that your solution is a bad one and also explain why it is.

    Quote Originally Posted by pixelink View Post
    Sisnce this is NOT a DB driven, I don't have t validate or commit anything to the datagrid in code.
    If you would have read the first link that I gave you, you would realize that you do not need a database to create and use ADO.NET objects.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  24. #24
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Weird Timer Behavior Issue

    Quote Originally Posted by pixelink View Post
    ITS FIXED... why are we still going on about this.
    Because we like to. After all, dead horses are easier to beat: They don't kick, they don't run away, etc.

    Still, I rather like the solution due to it's brevity. I'd still want to understand it better, and it has something to do with the editing model of the DGV. Sure, in this case you aren't really editing anything, but so what? It's a bad design to have a model that works one way for most things...and a different way for this one item, so a checkbox almost certainly works a whole lot like a textbox. Personally, I have never needed to dig into how DGVs work all that much, as I have tended to use them for pretty trivial things. Sometimes a grid is the right way to go, quite often it's just because you don't have time to do something better. Over the years, I have kept bumping into references to the edit model of the DGV, without ever really getting into it. I guess I'll continue that trend.
    My usual boring signature: Nothing

  25. #25
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: [RESOLVED] Weird Timer Behavior Issue

    Instead of…

    Code:
    dgScheduler.CurrentCell = Nothing
    Try…

    Code:
    dgScheduler.EndEdit

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