forms timers, events, and threading

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

    forms timers, events, and threading

    My WinForms app runs on the single default thread, and uses a single
    SqlConnection object for all queries. I need to use one or more timers to
    periodically execute some of them. My own testing indicates that the forms
    timer does not operate on its own thread and will not cause a query to be
    sent to use the single SqlConnection while another is being executed from
    the handling of other events. Is that right? I just want to be 100% sure
    before I commit to this design.

    Bob


  • Cor Ligthert

    #2
    Re: forms timers, events, and threading

    Bob,

    I am not completly sure of your question, however a thread does only one
    operation per operation. So when there is a longtime taking SQL operation
    by instance a dataadapter update, than that will first be done, before the
    timer event is catched.

    I hope this helps?

    Cor

    "Bob" <noone@nowhere. com>
    ...[color=blue]
    > My WinForms app runs on the single default thread, and uses a single
    > SqlConnection object for all queries. I need to use one or more timers to
    > periodically execute some of them. My own testing indicates that the forms
    > timer does not operate on its own thread and will not cause a query to be
    > sent to use the single SqlConnection while another is being executed from
    > the handling of other events. Is that right? I just want to be 100% sure
    > before I commit to this design.
    >
    > Bob
    >
    >[/color]


    Comment

    • Cor Ligthert

      #3
      Re: forms timers, events, and threading

      Bob,

      However before I forget it, why do you not test this kind of questions
      yourself when it is so important. A small test program is in my opinon fast
      enough made?

      sub Myprocedure Y
      threading.threa d.sleep(4000)
      end sub

      sub Timer
      test if it is throw by setting the timertick to 1000 miliseconds
      end sub

      Cor

      "Bob" <noone@nowhere. com>
      ...[color=blue]
      > My WinForms app runs on the single default thread, and uses a single
      > SqlConnection object for all queries. I need to use one or more timers to
      > periodically execute some of them. My own testing indicates that the forms
      > timer does not operate on its own thread and will not cause a query to be
      > sent to use the single SqlConnection while another is being executed from
      > the handling of other events. Is that right? I just want to be 100% sure
      > before I commit to this design.
      >
      > Bob
      >
      >[/color]


      Comment

      • Bob

        #4
        Re: forms timers, events, and threading

        You obviously missed the "my own testing" part of my message.

        Bob

        "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
        news:OMkB2ZuyEH A.2600@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Bob,
        >
        > However before I forget it, why do you not test this kind of questions
        > yourself when it is so important. A small test program is in my opinon[/color]
        fast[color=blue]
        > enough made?
        >
        > sub Myprocedure Y
        > threading.threa d.sleep(4000)
        > end sub
        >
        > sub Timer
        > test if it is throw by setting the timertick to 1000 miliseconds
        > end sub
        >
        > Cor[/color]


        Comment

        • Bob

          #5
          Re: forms timers, events, and threading

          I was wrong. 100% sure just wen to 0%.

          OK, new question. How can I get message boxes and other modal dialogues to
          stop timer tick events just like the interventing execution of other
          event-driven code code?

          I guess I'll have no choice now but to brew my own message boxes. What a
          pain.

          Bob

          "Bob" <noone@nowhere. com> wrote in message
          news:OC3YXUpyEH A.3820@TK2MSFTN GP11.phx.gbl...[color=blue]
          > My WinForms app runs on the single default thread, and uses a single
          > SqlConnection object for all queries. I need to use one or more timers to
          > periodically execute some of them. My own testing indicates that the forms
          > timer does not operate on its own thread and will not cause a query to be
          > sent to use the single SqlConnection while another is being executed from
          > the handling of other events. Is that right? I just want to be 100% sure
          > before I commit to this design.
          >
          > Bob
          >
          >[/color]


          Comment

          • Cor Ligthert

            #6
            Re: forms timers, events, and threading

            Bob,

            For which timer do you want to know this?

            Windows.forms.t imer
            or
            System.timers.t imer
            or
            System.threadin g.timer
            or
            Another one

            Cor
            [color=blue]
            >I was wrong. 100% sure just wen to 0%.
            >
            > OK, new question. How can I get message boxes and other modal dialogues to
            > stop timer tick events just like the interventing execution of other
            > event-driven code code?
            >
            > I guess I'll have no choice now but to brew my own message boxes. What a
            > pain.
            >
            > Bob
            >[/color]


            Comment

            • Kejpa

              #7
              Re: forms timers, events, and threading

              No, 100% sure of having right switched to 100% sure of having wrong. The
              100% sureness is there all the time <LOL>

              Can't you just before showing the message set the timer.enabled = false and
              reset it after showing the messagebox?
              I do that, but I'm using system.timer... .
              Maybe you should switch

              HTH
              /Kejpa


              "Bob" <noone@nowhere. com> wrote in message
              news:uy4uc4xyEH A.352@TK2MSFTNG P14.phx.gbl...[color=blue]
              > I was wrong. 100% sure just wen to 0%.
              >
              > OK, new question. How can I get message boxes and other modal dialogues to
              > stop timer tick events just like the interventing execution of other
              > event-driven code code?
              >
              > I guess I'll have no choice now but to brew my own message boxes. What a
              > pain.
              >
              > Bob
              >
              > "Bob" <noone@nowhere. com> wrote in message
              > news:OC3YXUpyEH A.3820@TK2MSFTN GP11.phx.gbl...[color=green]
              > > My WinForms app runs on the single default thread, and uses a single
              > > SqlConnection object for all queries. I need to use one or more timers[/color][/color]
              to[color=blue][color=green]
              > > periodically execute some of them. My own testing indicates that the[/color][/color]
              forms[color=blue][color=green]
              > > timer does not operate on its own thread and will not cause a query to[/color][/color]
              be[color=blue][color=green]
              > > sent to use the single SqlConnection while another is being executed[/color][/color]
              from[color=blue][color=green]
              > > the handling of other events. Is that right? I just want to be 100% sure
              > > before I commit to this design.
              > >
              > > Bob
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Bob

                #8
                Re: forms timers, events, and threading

                Cor and Kejpa,

                I really don't care what kind of timers I'm using, the only thing I'm after
                is that they don't tick while code is executing doing other events.
                Unfortunately I'm willing to bet none of them stop when a message box is
                popped up. That's what I'd like, but I'm probably just going to have to
                subclass all my timers, then make a custom message box class, and have the
                two classes communicate with static methods. I want to avoid adding this
                level of complexity if I can help it, but I'm not sure there's any other
                way.

                Bob


                Comment

                • Cor Ligthert

                  #9
                  Re: forms timers, events, and threading

                  Bob,

                  Complexity starts mostly that people do not know what they want to archieve
                  when they can tell that in a simple way, than mostly it is easy to get an
                  easy procedure as well. As far as I hear you, it seems an easy problem to
                  solve.

                  Cor

                  "Bob" <noone@nowhere. com>

                  ....[color=blue]
                  > Cor and Kejpa,
                  >
                  > I really don't care what kind of timers I'm using, the only thing I'm
                  > after
                  > is that they don't tick while code is executing doing other events.
                  > Unfortunately I'm willing to bet none of them stop when a message box is
                  > popped up. That's what I'd like, but I'm probably just going to have to
                  > subclass all my timers, then make a custom message box class, and have the
                  > two classes communicate with static methods. I want to avoid adding this
                  > level of complexity if I can help it, but I'm not sure there's any other
                  > way.
                  >
                  > Bob
                  >
                  >[/color]


                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: forms timers, events, and threading

                    Bob,
                    Didn't you ask this question on Sept 29th 2004?



                    I'm not sure if these threads will help you or not:






                    Rather then brew your own message box, I would have a well known object (a
                    Singleton, either the MainForm or my ApplicationCont ext singleton) have a
                    pair of methods that "locked" out the Timer events. Alternatively I would
                    code the Timer events so they were not reentrant (by checking a Static
                    variable in the method, the third link above has a sample).

                    Normally I simply code my Timer events so they are not reentrant.

                    Hope this helps
                    Jay



                    "Bob" <noone@nowhere. com> wrote in message
                    news:uy4uc4xyEH A.352@TK2MSFTNG P14.phx.gbl...[color=blue]
                    >I was wrong. 100% sure just wen to 0%.
                    >
                    > OK, new question. How can I get message boxes and other modal dialogues to
                    > stop timer tick events just like the interventing execution of other
                    > event-driven code code?
                    >
                    > I guess I'll have no choice now but to brew my own message boxes. What a
                    > pain.
                    >
                    > Bob
                    >
                    > "Bob" <noone@nowhere. com> wrote in message
                    > news:OC3YXUpyEH A.3820@TK2MSFTN GP11.phx.gbl...[color=green]
                    >> My WinForms app runs on the single default thread, and uses a single
                    >> SqlConnection object for all queries. I need to use one or more timers to
                    >> periodically execute some of them. My own testing indicates that the
                    >> forms
                    >> timer does not operate on its own thread and will not cause a query to be
                    >> sent to use the single SqlConnection while another is being executed from
                    >> the handling of other events. Is that right? I just want to be 100% sure
                    >> before I commit to this design.
                    >>
                    >> Bob
                    >>
                    >>[/color]
                    >
                    >[/color]


                    Comment

                    • Cor Ligthert

                      #11
                      Re: forms timers, events, and threading

                      Bob,

                      Maybe can this sample that I made for you clear it better.

                      \\\Needs one button on a form
                      Private start As Integer
                      Private WithEvents mytimer As New Windows.Forms.T imer
                      Private Sub Button1_Click(B yVal sender _
                      As Object, ByVal e As System.EventArg s) _
                      Handles Button1.Click
                      mytimer.Enabled = True
                      mytimer.Interva l = 2000
                      start = Environment.Tic kCount
                      Threading.Threa d.Sleep(1000)
                      Threading.Threa d.Sleep(1000)
                      Threading.Threa d.Sleep(1000)
                      Threading.Threa d.Sleep(1000)
                      End Sub

                      Private Sub mytimer_Tick(By Val sender As Object, ByVal _
                      e As System.EventArg s) Handles mytimer.Tick
                      mytimer.Enabled = False
                      MessageBox.Show ((Environment.T ickCount - _
                      start).ToString )
                      End Sub
                      ///
                      "Bob" <noone@nowhere. com>
                      [color=blue]
                      > Cor and Kejpa,
                      >
                      > I really don't care what kind of timers I'm using, the only thing I'm
                      > after
                      > is that they don't tick while code is executing doing other events.
                      > Unfortunately I'm willing to bet none of them stop when a message box is
                      > popped up. That's what I'd like, but I'm probably just going to have to
                      > subclass all my timers, then make a custom message box class, and have the
                      > two classes communicate with static methods. I want to avoid adding this
                      > level of complexity if I can help it, but I'm not sure there's any other
                      > way.
                      >
                      > Bob
                      >
                      >[/color]


                      Comment

                      • Cor Ligthert

                        #12
                        Re: forms timers, events, and threading

                        Bob,

                        Because that Jay showed us that previous thread, I think that I could make a
                        better sample.

                        \\\
                        Private WithEvents mytimer As New Windows.Forms.T imer
                        Private Sub Form4_Load(ByVa l sender As Object, _
                        ByVal e As System.EventArg s) Handles MyBase.Load
                        mytimer.Enabled = True
                        mytimer.Interva l = 5000
                        End Sub
                        Private Sub mytimer_Tick(By Val sender As Object, ByVal _
                        e As System.EventArg s) Handles mytimer.Tick
                        Dim start As Integer
                        mytimer.Enabled = False
                        start = Environment.Tic kCount
                        MessageBox.Show ((Now.ToString( "mm-ss")))
                        mytimer.Interva l = 5000 - _
                        (Environment.Ti ckCount - start)
                        mytimer.Enabled = True
                        start = Environment.Tic kCount
                        End Sub
                        ///
                        You have to click inside the 5 seconds for this button, and ending the
                        program can give an error however it is just to show you this.

                        This accoording what I now understand from your problem. Know that you never
                        can use in Net1.1 one ADONET connection more than one time.

                        I hope this helps?

                        Cor

                        "Bob" <noone@nowhere. com>

                        [color=blue]
                        > Cor and Kejpa,
                        >
                        > I really don't care what kind of timers I'm using, the only thing I'm
                        > after
                        > is that they don't tick while code is executing doing other events.
                        > Unfortunately I'm willing to bet none of them stop when a message box is
                        > popped up. That's what I'd like, but I'm probably just going to have to
                        > subclass all my timers, then make a custom message box class, and have the
                        > two classes communicate with static methods. I want to avoid adding this
                        > level of complexity if I can help it, but I'm not sure there's any other
                        > way.
                        >
                        > Bob
                        >
                        >[/color]


                        Comment

                        • Bob

                          #13
                          Re: forms timers, events, and threading

                          Yes, I posted a message on a previous "thread" (pun intended) but it was
                          more about whether or not to use threading at all. I've pretty much settled
                          on not using threading if I can help it, but ran into the message box
                          problem in the middle of finally committing to the single-thread design.
                          Unfortunately blocking reentry on the instance level is not enough, there
                          may be many timers; I'm thinking of using a static property. That takes me
                          90% of the way there.... actually that might be enough and I might not have
                          to worry about subclassing message boxes at all.

                          Thanks for helping me think a bit.
                          Bob

                          "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                          news:eYPMsvzyEH A.4004@tk2msftn gp13.phx.gbl...[color=blue]
                          > Bob,
                          > Didn't you ask this question on Sept 29th 2004?
                          >
                          >[/color]
                          http://groups.google.com/groups?hl=e...TNGP11.phx.gbl[color=blue]
                          >
                          > I'm not sure if these threads will help you or not:
                          >
                          >[/color]
                          http://groups.google.com/groups?hl=e...eb%25404ax.com[color=blue]
                          >
                          >[/color]
                          http://groups.google.com/groups?hl=e...TNGP12.phx.gbl[color=blue]
                          >
                          >
                          > Rather then brew your own message box, I would have a well known object (a
                          > Singleton, either the MainForm or my ApplicationCont ext singleton) have a
                          > pair of methods that "locked" out the Timer events. Alternatively I would
                          > code the Timer events so they were not reentrant (by checking a Static
                          > variable in the method, the third link above has a sample).
                          >
                          > Normally I simply code my Timer events so they are not reentrant.
                          >
                          > Hope this helps
                          > Jay[/color]


                          Comment

                          • Jay B. Harlow [MVP - Outlook]

                            #14
                            Re: forms timers, events, and threading

                            Bob,[color=blue]
                            > may be many timers; I'm thinking of using a static property.[/color]
                            A static property of what?

                            In the case of many timers, I would consider making a static (Shared)
                            property of a custom Timer class. That enabled raising Timer.Tick events.

                            Something like:

                            Public Class TimerEx
                            Inherits System.Windows. Forms.Timer

                            Private Shared m_eventsEnabled As Boolean

                            Public Shared Property EventsEnabled() As Boolean
                            Get
                            Return m_eventsEnabled
                            End Get
                            Set(ByVal value As Boolean)
                            m_eventsEnabled = value
                            End Set
                            End Property

                            Protected Overrides Sub OnTick(ByVal e As System.EventArg s)
                            If m_eventsEnabled Then
                            MyBase.OnTick(e )
                            End If
                            End Sub

                            End Class

                            Then on my forms I would use a TimerEx instead of Timer.

                            When TimerEx.EventsE nabled is False, no timer will raise its events, if
                            TimerEx.EventsE nabled is True then all timers will raise their events.

                            However! I normally favor Threading over using Timers to mimic Threading.

                            Hope this helps
                            Jay

                            "Bob" <noone@nowhere. com> wrote in message
                            news:Owrz8N0yEH A.1392@TK2MSFTN GP14.phx.gbl...[color=blue]
                            > Yes, I posted a message on a previous "thread" (pun intended) but it was
                            > more about whether or not to use threading at all. I've pretty much
                            > settled
                            > on not using threading if I can help it, but ran into the message box
                            > problem in the middle of finally committing to the single-thread design.
                            > Unfortunately blocking reentry on the instance level is not enough, there
                            > may be many timers; I'm thinking of using a static property. That takes me
                            > 90% of the way there.... actually that might be enough and I might not
                            > have
                            > to worry about subclassing message boxes at all.
                            >
                            > Thanks for helping me think a bit.
                            > Bob
                            >
                            > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                            > news:eYPMsvzyEH A.4004@tk2msftn gp13.phx.gbl...[color=green]
                            >> Bob,
                            >> Didn't you ask this question on Sept 29th 2004?
                            >>
                            >>[/color]
                            > http://groups.google.com/groups?hl=e...TNGP11.phx.gbl[color=green]
                            >>
                            >> I'm not sure if these threads will help you or not:
                            >>
                            >>[/color]
                            > http://groups.google.com/groups?hl=e...eb%25404ax.com[color=green]
                            >>
                            >>[/color]
                            > http://groups.google.com/groups?hl=e...TNGP12.phx.gbl[color=green]
                            >>
                            >>
                            >> Rather then brew your own message box, I would have a well known object
                            >> (a
                            >> Singleton, either the MainForm or my ApplicationCont ext singleton) have a
                            >> pair of methods that "locked" out the Timer events. Alternatively I would
                            >> code the Timer events so they were not reentrant (by checking a Static
                            >> variable in the method, the third link above has a sample).
                            >>
                            >> Normally I simply code my Timer events so they are not reentrant.
                            >>
                            >> Hope this helps
                            >> Jay[/color]
                            >
                            >[/color]


                            Comment

                            Working...