Results 1 to 12 of 12

Thread: Asynchronous action?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    17

    Asynchronous action?

    Hi all,

    I have an application with will moving the record and display in the chart by click a button. Does anyone know how make sure the action is asynchronous?What i mean here is how to make sure the next action is going after the action before completely done.

    Any tips are welcome.

    regards
    lswang

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Asynchronous action?

    Post your code

  3. #3
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Asynchronous action?

    simplest way is disable the button at the beginning of your code and enable it again at the end.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    17

    Re: Asynchronous action?

    Hi
    Lord Orwell, thanks for your suggestion.And Leinail31, below is my part of code to implement the display chart action when click on button.

    Code:
    Private Sub Plot_QueryChart(action As Integer)
    Dim data As Integer, MoveValue As Integer
    
    
    On Error GoTo errorhandler
    
    If Not QueryRecordSet.EOF Or Not QueryRecordSet.BOF Then
    
        QueryRecordSet.MoveLast
        JJ = QueryRecordSet.RecordCount
        '**********************load all record*********************
        If action = 0 Then
              '*****get recordset
               If JJ > 60 Then
                  QueryRecordSet.Move -cTotal_Brecord   '***to search record
                  EmptyRow = 0
                  '***currentRecord is to calculate how many record should move
                  '***cTotal_Brecord is const variable
                  currentRecord = cTotal_Brecord
                  '***menuMoveFlag aim to disable some menu button                
                  menuMoveFlag False, True
               Else
                  QueryRecordSet.MoveFirst
                  EmptyRow = cTotal_Brecord - JJ
                  currentRecord = 0
                  menuMoveFlag False, False
               End If
            
        '*************move previous 1 record*********************
        ElseIf action = 1 Then
            If currentRecord + cMovePreOne < JJ Then
                currentRecord = currentRecord + cMovePreOne
                QueryRecordSet.Move -currentRecord
                EmptyRow = 0
                    If currentRecord + cMovePreOne >= JJ Then
                        menuMoveFlag True, False
                    Else
                        menuMoveFlag True, True
                    End If
            End If
            
        '*************move previous page*************************
        ElseIf action = 2 Then
            If (currentRecord + movePrePage) <= JJ Then
                currentRecord = currentRecord + movePrePage
                QueryRecordSet.Move -currentRecord
                EmptyRow = 0
                    If (currentRecord + 1) >= JJ Then
                        menuMoveFlag True, False
                    Else
                        menuMoveFlag True, True
                    End If
            Else
                currentRecord = JJ - 1
                QueryRecordSet.Move -currentRecord
                EmptyRow = 0
                menuMoveFlag True, False
            End If
            
        '***************move next 1 record*************************
        ElseIf action = 3 Then
            If currentRecord - cMoveNextOne >= cTotal_Brecord Then
                currentRecord = currentRecord - cMoveNextOne
                QueryRecordSet.Move -currentRecord
                EmptyRow = 0
                    If currentRecord - cMoveNextOne < 60 Then
                        menuMoveFlag False, True
                    Else
                        menuMoveFlag True, True
                    End If
            End If
            
        '**************move next page*****************************
        ElseIf action = 4 Then
            If currentRecord - moveNextPage >= cTotal_Brecord Then
                currentRecord = currentRecord - moveNextPage
                QueryRecordSet.Move -currentRecord
                EmptyRow = 0
                    If currentRecord = 60 Then
                        menuMoveFlag False, True
                    Else
                        menuMoveFlag True, True
                    End If
            Else
                QueryRecordSet.Move -moveNextPage
                currentRecord = moveNextPage
                EmptyRow = 0
                menuMoveFlag False, True
            End If
            
        '**************search record******************************
        ElseIf action = 5 Then
            '***SearchLocationPiont is the location of record found
            '***CurrentToTalPoint is the total record count
            If SearchLocationPoint - (CurrentToTalPoint - JJ) > JJ Then Exit Sub
             
            If SearchLocationPoint - (CurrentToTalPoint - JJ) <= JJ Then
                currentRecord = SearchLocationPoint - (CurrentToTalPoint - JJ)
                If currentRecord < cTotal_Brecord Then      '
                    currentRecord = moveNextPage
                    QueryRecordSet.Move -cTotal_Brecord
                    EmptyRow = 0
                    menuMoveFlag False, True
                ElseIf currentRecord > JJ - cTotal_Brecord Then
                    currentRecord = JJ - 1
                    QueryRecordSet.Move -currentRecord
                    EmptyRow = 0
                    menuMoveFlag True, False
                Else
                    QueryRecordSet.Move -currentRecord
                    EmptyRow = 0
                    menuMoveFlag True, True
                End If
            End If
        
        '*********move to First Page********************************
        ElseIf action = 6 Then
            currentRecord = JJ - 1
            QueryRecordSet.Move -currentRecord
            EmptyRow = 0
            menuMoveFlag True, False
        
        '********move to Last page*********************************
        ElseIf action = 7 Then
            '***moveNextPage is a variable
            QueryRecordSet.Move -moveNextPage
            currentRecord = moveNextPage
            EmptyRow = 0
            menuMoveFlag False, True
        
        End If
        DoEvents
        
        '**************insert data,plot chart************************
            For data = 1 To cTotal_Brecord
                If data > EmptyRow Then
                    If Not (QueryRecordSet.BOF) Then
                    Debug.Print (Format$((QueryRecordSet("RDate")), "dd/mm/yy") & " " & Format$((QueryRecordSet("RTime")), "hh:mm:ss"))
                       QueryArrValues(data, 1) = Format$((QueryRecordSet("RDate")), "dd/mm/yy") & " " & Format$((QueryRecordSet("RTime")), "hh:mm:ss") ' Labels
                       QueryArrValues(data, 2) = IIf(IsNull(QueryRecordSet(PreFieldString)), 0, QueryRecordSet(PreFieldString)) ' Series 1 values.
                       QueryArrValues(data, 3) = IIf(IsNull(QueryRecordSet(ActFieldString)), 0, QueryRecordSet(ActFieldString)) ' Series 2 values.
            
                       QueryRecordSet.MoveNext
                    End If
                Else
                       QueryArrValues(data, 1) = " " ' Labels
                       QueryArrValues(data, 2) = 0 ' Series 1 values.
                       QueryArrValues(data, 3) = 0 ' Series 2 values.
                End If
            Next data
            DoEvents
            MSChart1.TitleText = trendName & " -- " & trendPreriod
            MSChart1.ChartData = QueryArrValues
            MSChart1.Column = 1: MSChart1.ColumnLabel = "Pre."
            MSChart1.Column = 2: MSChart1.ColumnLabel = "Act."
            
            
            
        '*******************************************************************************
    End If
    
    Exit Sub
    errorhandler:
    Call ELogger("Plot_Chart" & "-" & action)
    Resume Next
        
    End Sub

  5. #5
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Asynchronous action?

    You wouldn't have the problem if that doevents was not in there. why is it there?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    17

    Re: Asynchronous action?

    hi

    Actually my whole application are containts 4 timers and will run every 1.5 sec and 5 sec.So, i need to use doevents to release my cpu usage,and avoid lack up of application.Does it make a problem for my application if i put the doevents there?


    regards
    lswang

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Asynchronous action?

    yes, but you can cut down on the problems with global variables at the start of each of your subroutines. Set it to a value when your sub starts, and the line before that checkes to see if the value is already set. If so, exit sub/function. Last line of sub (or any place you have an exit sub) you reset the value. This prevents a subroutine from running more than once at the same time.
    As for having all of those timers. This is a bad idea. A program never needs more than one. You use counting loops with static variables to ensure that you have the time interval you want. I am positive you are doing this already since you have intervals greater than 100ms. This is the biggest problem. Timers are unreliable and if you have more than one firing at the same time, occasionally (but quite often) you will lose event triggers.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    17

    Re: Asynchronous action?

    Hi,

    Code:
    Set it to a value when your sub starts, and the line before that checkes to see if the value is already set. If so, exit sub/function. Last line of sub (or any place you have an exit sub) you reset the value.
    Did you mean set a flag before check the recordset and reset the flag?


    Code:
    As for having all of those timers. This is a bad idea.
    I had no choice but have to do it because i need to get some data due com port and at the same time i need to plot the chart according to the value which i got. And yes, as you said, i may be lose the events triggers while the other timers run toghter.

    Can you please briefly how can i use counting loops to get the intervals time i need in condition that i have 3 action (currently using timers to run the action)need to complete at the same time..


    thanks

    best regards
    lswang

  9. #9
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Asynchronous action?

    yes it just requires a global variable for each one.
    Code:
    (inside timer1_timer with interval of 100)
    a = a + 1
    b = b + 1
    c = c + 1
    if a = 100 then a = 0:  Call CodeThatTriggersEvery10Seconds
    if b = 10 then b = 0:  Call CodeThatTriggersEverySecond
    if c = 450 then c = 0 etc.
    end sub
    get the picture? You never need more than one timer, and it can be demonstrated pretty easily that as few as 3 or 4 timers will miss events.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Asynchronous action?

    Quote Originally Posted by cocoon_wls
    Code:
    As for having all of those timers. This is a bad idea.
    I had no choice but have to do it because i need to get some data due com port and at the same time i need to plot the chart according to the value which i got.
    The MSComm control is event-driven, you don't need timers. If you get your incoming data in the MSComm1_OnComm event, the event will fire when there's data to get, regardless of what else you're doing. If you're using a timer to look at MSComm1.Input, you're using the control the wrong way. There's almost never a good reason to poll an interrupting asynchronous input.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2007
    Posts
    17

    Re: Asynchronous action?

    hi,

    First,thanks for Loar Orvwell, i have got the pic and then i will try it in my program.So, that mean if i running these code in my application, the percentage of lost event triggers will be decrease,am I right?

    Second, for the Al42,thanks for your suggestion.Basically, in my application i already running the concept you are mention, which is get data while the comm event fired.But at the same time i need another timer to get the data and continue my next action if i cant receive the complete data from comm in 100ms.Example,i need to get 10 char from comm in 100ms. If i cant complete get all the data in 100ms, i will give up and continue my next action (in my condition, i cant wait untill i get the complete data, i must move to next action after 100ms).

  12. #12
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Asynchronous action?

    In theory, it should be more reliable.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

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