Creation of dynamic controls - Timers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry Spence1

    Creation of dynamic controls - Timers

    I am trying to create timers on demand by doing the following:

    Dim NewTimer As New System.Timers.T imer
    NewTimer.Interv al = 10000
    AddHandler NewTimer.Elapse d, AddressOf Timeup
    NewTimer.Enable d = True
    NewTimer.Start( )

    My problem then happens in the TimeUp routine

    Normally, to get the name of the control that has been fired you would do:

    Private Sub Timeup(ByVal sender As System.Object, ByVal e As
    System.Timers.E lapsedEventArgs )

    Dim mytimer = DirectCast(send er, Control).Name.T oString

    End Sub

    The problem is that timers do not have a property called 'Name'. How can I
    reference which timer is being fired?

    Thanks

    -Jerry


  • Cor Ligthert [MVP]

    #2
    Re: Creation of dynamic controls - Timers

    Jerry,

    Is there a special reason that you use the system.timers.t imer. I like more
    the simple use of the normal forms timer. Therefore I have made a sample
    with that one.

    It is based on just adding that Name that you want to the class. I have set
    it on our website



    I hope this helps,

    Cor


    Comment

    • Armin Zingler

      #3
      Re: Creation of dynamic controls - Timers

      "Jerry Spence1" <jerry.spence@s omewhere.com> schrieb[color=blue]
      > I am trying to create timers on demand by doing the following:
      >
      > Dim NewTimer As New System.Timers.T imer
      > NewTimer.Interv al = 10000
      > AddHandler NewTimer.Elapse d, AddressOf Timeup
      > NewTimer.Enable d = True
      > NewTimer.Start( )
      >
      > My problem then happens in the TimeUp routine
      >
      > Normally, to get the name of the control that has been fired you
      > would do:
      >
      > Private Sub Timeup(ByVal sender As System.Object, ByVal e As
      > System.Timers.E lapsedEventArgs )
      >
      > Dim mytimer = DirectCast(send er, Control).Name.T oString
      >
      > End Sub
      >
      > The problem is that timers do not have a property called 'Name'. How
      > can I reference which timer is being fired?[/color]


      In addition to Cor,

      an object can be identified by it's reference. If you don't store the
      reference permanently, you can't identify it later. I don't know where you
      create the Timer and where you need it. If it's in a Form, store it in a
      Form's field instead of in a local variable:

      'outside all subs (= a field in the form):
      Dim NewTimer As System.Timers.T imer

      Later use the 'Is' operator to see if it's a certain timer:

      If sender is NewTimer Then
      '...
      elseif sender is AnotherTimerIfY ouHaveOne then
      '...
      end if


      Armin

      Comment

      • Jerry Spence1

        #4
        Re: Creation of dynamic controls - Timers

        Thanks Cor

        I'm not sure what you mean by the 'normal' timer. There is a timer in the
        components tab, and there is one in the forms tab - both seem to be
        system.timers.t imer. What's the difference?


        -Jerry

        "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
        news:O8NqteWpFH A.3408@tk2msftn gp13.phx.gbl...[color=blue]
        > Jerry,
        >
        > Is there a special reason that you use the system.timers.t imer. I like
        > more the simple use of the normal forms timer. Therefore I have made a
        > sample with that one.
        >
        > It is based on just adding that Name that you want to the class. I have
        > set it on our website
        >
        > http://www.windowsformsdatagridhelp....c-61641f5c8d9d
        >
        > I hope this helps,
        >
        > Cor
        >[/color]


        Comment

        • Jerry Spence1

          #5
          Re: Creation of dynamic controls - Timers

          Thanks Armin

          If it's reference is stored outside all Subs, then how can I create it
          dynamically?

          -Jerry

          "Armin Zingler" <az.nospam@free net.de> wrote in message
          news:uSJC72WpFH A.420@TK2MSFTNG P09.phx.gbl...[color=blue]
          > "Jerry Spence1" <jerry.spence@s omewhere.com> schrieb[color=green]
          >> I am trying to create timers on demand by doing the following:
          >>
          >> Dim NewTimer As New System.Timers.T imer
          >> NewTimer.Interv al = 10000
          >> AddHandler NewTimer.Elapse d, AddressOf Timeup
          >> NewTimer.Enable d = True
          >> NewTimer.Start( )
          >>
          >> My problem then happens in the TimeUp routine
          >>
          >> Normally, to get the name of the control that has been fired you
          >> would do:
          >>
          >> Private Sub Timeup(ByVal sender As System.Object, ByVal e As
          >> System.Timers.E lapsedEventArgs )
          >>
          >> Dim mytimer = DirectCast(send er, Control).Name.T oString
          >>
          >> End Sub
          >>
          >> The problem is that timers do not have a property called 'Name'. How
          >> can I reference which timer is being fired?[/color]
          >
          >
          > In addition to Cor,
          >
          > an object can be identified by it's reference. If you don't store the
          > reference permanently, you can't identify it later. I don't know where you
          > create the Timer and where you need it. If it's in a Form, store it in a
          > Form's field instead of in a local variable:
          >
          > 'outside all subs (= a field in the form):
          > Dim NewTimer As System.Timers.T imer
          >
          > Later use the 'Is' operator to see if it's a certain timer:
          >
          > If sender is NewTimer Then
          > '...
          > elseif sender is AnotherTimerIfY ouHaveOne then
          > '...
          > end if
          >
          >
          > Armin
          >[/color]


          Comment

          • Jerry Spence1

            #6
            Re: Creation of dynamic controls - Timers

            Forget my last Cor - I've just looked a bit closer. Yes they are different.

            Thanks for the example you did - you're the best!

            -Jerry


            "Jerry Spence1" <jerry.spence@s omewhere.com> wrote in message
            news:430707d9$0 $17495$ed2e19e4 @ptn-nntp-reader04.plus.n et...[color=blue]
            > Thanks Cor
            >
            > I'm not sure what you mean by the 'normal' timer. There is a timer in the
            > components tab, and there is one in the forms tab - both seem to be
            > system.timers.t imer. What's the difference?
            >
            >
            > -Jerry
            >
            > "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
            > news:O8NqteWpFH A.3408@tk2msftn gp13.phx.gbl...[color=green]
            >> Jerry,
            >>
            >> Is there a special reason that you use the system.timers.t imer. I like
            >> more the simple use of the normal forms timer. Therefore I have made a
            >> sample with that one.
            >>
            >> It is based on just adding that Name that you want to the class. I have
            >> set it on our website
            >>
            >> http://www.windowsformsdatagridhelp....c-61641f5c8d9d
            >>
            >> I hope this helps,
            >>
            >> Cor
            >>[/color]
            >
            >[/color]


            Comment

            • Armin Zingler

              #7
              Re: Creation of dynamic controls - Timers

              "Jerry Spence1" <jerry.spence@s omewhere.com> schrieb[color=blue]
              > Thanks Armin
              >
              > If it's reference is stored outside all Subs, then how can I create
              > it dynamically?[/color]

              You can add them to an array list.

              What I don't understand: If the number of Timers is not known at design
              time, the reference is already the "key" to the timer. The 'sender' is
              already pointing to the timer that fired the event. Why do you need another
              way of identification? I would probably understand it if the number of
              Timers was determined at design time. Then you could write code
              distinguishing between Timers A, B and C, but as you want to add Timers
              dynamically, this is obviously not the case. So, what's your intention?


              Armin

              Comment

              • Jerry Spence1

                #8
                Re: Creation of dynamic controls - Timers

                Sorry - my knowledge of VB.Net isn't quite up to scratch to interpret what
                you are saying about the 'key' to the timer.

                The actual problem is that I have a number of IP based door swipes (access
                control). When someone swipes a card, my program receives the card details
                and processes it and opens the door (or not). It also creates a component
                which receives and records an MPEG4 stream from a video source. That is all
                working OK.

                I also create a timer and set it to a value which is based on the length of
                time that I want the video to be recorded of the person who swiped the card.
                When the timer fires, it has to turn off the video stream from that
                particular camera. The dynamic part is that I have no idea how many people
                are likely to be swiping their cards at the same time, so I need to create
                and destroy timers at will. On large installations this could be more than
                20 (eg. in a college at home time). When the timer fires it will end up at a
                common point, so I need to be able to identify it so that I know which video
                stream to cut off.

                - Jerry


                "Armin Zingler" <az.nospam@free net.de> wrote in message
                news:eZFkqlXpFH A.1412@TK2MSFTN GP09.phx.gbl...[color=blue]
                > "Jerry Spence1" <jerry.spence@s omewhere.com> schrieb[color=green]
                >> Thanks Armin
                >>
                >> If it's reference is stored outside all Subs, then how can I create
                >> it dynamically?[/color]
                >
                > You can add them to an array list.
                >
                > What I don't understand: If the number of Timers is not known at design
                > time, the reference is already the "key" to the timer. The 'sender' is
                > already pointing to the timer that fired the event. Why do you need
                > another
                > way of identification? I would probably understand it if the number of
                > Timers was determined at design time. Then you could write code
                > distinguishing between Timers A, B and C, but as you want to add Timers
                > dynamically, this is obviously not the case. So, what's your intention?
                >
                >
                > Armin
                >[/color]


                Comment

                • Armin Zingler

                  #9
                  Re: Creation of dynamic controls - Timers

                  "Jerry Spence1" <jerry.spence@s omewhere.com> schrieb[color=blue]
                  > Sorry - my knowledge of VB.Net isn't quite up to scratch to
                  > interpret what you are saying about the 'key' to the timer.
                  >
                  > The actual problem is that I have a number of IP based door swipes
                  > (access control). When someone swipes a card, my program receives
                  > the card details and processes it and opens the door (or not). It
                  > also creates a component which receives and records an MPEG4 stream
                  > from a video source. That is all working OK.
                  >
                  > I also create a timer and set it to a value which is based on the
                  > length of time that I want the video to be recorded of the person
                  > who swiped the card. When the timer fires, it has to turn off the
                  > video stream from that particular camera. The dynamic part is that I
                  > have no idea how many people are likely to be swiping their cards at
                  > the same time, so I need to create and destroy timers at will. On
                  > large installations this could be more than 20 (eg. in a college at
                  > home time). When the timer fires it will end up at a common point,
                  > so I need to be able to identify it so that I know which video
                  > stream to cut off.[/color]


                  I don't understand the video part, but... ;-)

                  .... I think you are looking for an _association_ between the timer and the
                  video stream. You can inherit from System.Windows. Forms.Timer and add a
                  property referencing the video stream or whatever object you created for
                  this purpose.

                  class VideoTimer
                  inherits System.Windows. Forms.Timer

                  public readonly additionaldata as ...

                  public sub new(byval additionaldata as ...)
                  me.additionalda ta = additionaldata
                  end sub

                  end class

                  Instead of creating a System.Windows. Forms.Timer, create an instance of your
                  class, and add it to the arraylist I suggested before:

                  dim vt as videotimer
                  vt = new VideoTimer(Vide oStream)
                  al.add(vt)


                  If you get the Timer's event, you can cast to VideoTimer and access the data
                  related to it:

                  dim vt as videotimer
                  vt = directcast(send er, videotimer)
                  vt.additionalda ta.<whatever>



                  Armin

                  Comment

                  Working...