Timer blocking problems...

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

    #1

    Timer blocking problems...

    Hi to all,

    i have a form with 2 System.Windows. Forms.Timer objects.
    One fire every 5 seconds and the other every 10 seconds,
    the both take actions in two hashtables declared in same form.

    When timers fire, main form is somewhat blocking until timers finish
    their job, (socket operations). (Imagine to move the form by it's caption
    bar and it somewhat freeze when timers fire...)

    How i can design this so this blocking do not happen ?

    Imagine that i am building an application like Sql Server's
    Enterprise Manager, it continuously talks(in the background)
    via sockets so it can display it's registered servers' status but it allows
    you
    to work smoothly with it in the foreground.

    I tried to spawn a thread to accomplish that
    (ThreadPool.Que ueUserWorkItem( new WaitCallback(Ch eckPendingServa nts));)

    but the problem remains.

    I also thought that because these 2 timer routines take actions on the
    forms' hashtables,
    the code running in these 2 threads will have problems to communicate with
    form's main thread
    and needs some delegate mechanism to do so.
    I also build this too but problem remains...

    My last thought is to create a second form with these 2 timers and hide it,
    this form will do all work and just take actions on the -now- public
    hashtables
    on the main form.

    Does any other idea comes to mind ?

    Thanks a lot


    anthonyb



  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Timer blocking problems...

    Anthony,

    Using another thread is definitely the way to go. However, you don't
    want to use System.Windows. Forms.Timer, as that will give you the
    notifications on the UI thread, which is causing a problem.

    The Timer class in the System.Timer namespace will help you. You can
    create it on your UI thread, but the event notifications will come in on
    another thread. You can then handle all of your processing in this event
    handler. However, when updating the UI, you will have to use calls to
    delegates through an Invoke method on a control in the UI so that the calls
    to update the UI are on the UI thread. Otherwise, do all of the processing
    on the other thread.

    Also, I believe that the event is fired on a thread from the thread
    pool. If your processing is long running, then you might want to throw the
    processing in another thread (so that you don't tie up the thread pool).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Anthony Boudouvas" <anthonyb@media trel.com> wrote in message
    news:OH5v4cqsDH A.2508@TK2MSFTN GP12.phx.gbl...[color=blue]
    > Hi to all,
    >
    > i have a form with 2 System.Windows. Forms.Timer objects.
    > One fire every 5 seconds and the other every 10 seconds,
    > the both take actions in two hashtables declared in same form.
    >
    > When timers fire, main form is somewhat blocking until timers finish
    > their job, (socket operations). (Imagine to move the form by it's caption
    > bar and it somewhat freeze when timers fire...)
    >
    > How i can design this so this blocking do not happen ?
    >
    > Imagine that i am building an application like Sql Server's
    > Enterprise Manager, it continuously talks(in the background)
    > via sockets so it can display it's registered servers' status but it[/color]
    allows[color=blue]
    > you
    > to work smoothly with it in the foreground.
    >
    > I tried to spawn a thread to accomplish that
    > (ThreadPool.Que ueUserWorkItem( new WaitCallback(Ch eckPendingServa nts));)
    >
    > but the problem remains.
    >
    > I also thought that because these 2 timer routines take actions on the
    > forms' hashtables,
    > the code running in these 2 threads will have problems to communicate with
    > form's main thread
    > and needs some delegate mechanism to do so.
    > I also build this too but problem remains...
    >
    > My last thought is to create a second form with these 2 timers and hide[/color]
    it,[color=blue]
    > this form will do all work and just take actions on the -now- public
    > hashtables
    > on the main form.
    >
    > Does any other idea comes to mind ?
    >
    > Thanks a lot
    >
    >
    > anthonyb
    >
    >
    >[/color]


    Comment

    • anthonyb

      #3
      Re: Timer blocking problems...

      [color=blue]
      > The Timer class in the System.Timer namespace will help you. You can
      > create it on your UI thread, but the event notifications will come in on
      > another thread. You can then handle all of your processing in this event
      > handler. However, when updating the UI, you will have to use calls to
      > delegates through an Invoke method on a control in the UI so that the[/color]
      calls[color=blue]
      > to update the UI are on the UI thread. Otherwise, do all of the[/color]
      processing[color=blue]
      > on the other thread.[/color]

      I did try that before my post here and i was facing the same problem.
      I drop a Timer control in my form, not one of the Winforms controls but the
      other in the component palette which is a System.Timer control
      as you say.
      Nothing changed. I do not know if you mean to create a System.Timer
      programmaticall y and create and connect an event handler in it,
      but i think that is the same as the System.Timer control on the form,
      isn't it ?

      As with the solution of the hidden secondary form, what is your opinion
      in that?


      Thanks a lot for the help!

      anthonyb

      [color=blue]
      >
      > Also, I believe that the event is fired on a thread from the thread
      > pool. If your processing is long running, then you might want to throw[/color]
      the[color=blue]
      > processing in another thread (so that you don't tie up the thread pool).[/color]

      No it isn't a long running process, it is just a try/catch, creating a
      TcpClient
      on a apecified computer:port and it last below of 2 seconds.




      [color=blue]
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Anthony Boudouvas" <anthonyb@media trel.com> wrote in message
      > news:OH5v4cqsDH A.2508@TK2MSFTN GP12.phx.gbl...[color=green]
      > > Hi to all,
      > >
      > > i have a form with 2 System.Windows. Forms.Timer objects.
      > > One fire every 5 seconds and the other every 10 seconds,
      > > the both take actions in two hashtables declared in same form.
      > >
      > > When timers fire, main form is somewhat blocking until timers finish
      > > their job, (socket operations). (Imagine to move the form by it's[/color][/color]
      caption[color=blue][color=green]
      > > bar and it somewhat freeze when timers fire...)
      > >
      > > How i can design this so this blocking do not happen ?
      > >
      > > Imagine that i am building an application like Sql Server's
      > > Enterprise Manager, it continuously talks(in the background)
      > > via sockets so it can display it's registered servers' status but it[/color]
      > allows[color=green]
      > > you
      > > to work smoothly with it in the foreground.
      > >
      > > I tried to spawn a thread to accomplish that
      > > (ThreadPool.Que ueUserWorkItem( new WaitCallback(Ch eckPendingServa nts));)
      > >
      > > but the problem remains.
      > >
      > > I also thought that because these 2 timer routines take actions on the
      > > forms' hashtables,
      > > the code running in these 2 threads will have problems to communicate[/color][/color]
      with[color=blue][color=green]
      > > form's main thread
      > > and needs some delegate mechanism to do so.
      > > I also build this too but problem remains...
      > >
      > > My last thought is to create a second form with these 2 timers and hide[/color]
      > it,[color=green]
      > > this form will do all work and just take actions on the -now- public
      > > hashtables
      > > on the main form.
      > >
      > > Does any other idea comes to mind ?
      > >
      > > Thanks a lot
      > >
      > >
      > > anthonyb
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Timer blocking problems...

        Anthony,

        You could always spawn your own thread and then call Sleep on it for the
        specified amount of time. Does that work for you?


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "anthonyb" <anthonyb@media trel.com> wrote in message
        news:%23Z7Hlcss DHA.2520@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        >[color=green]
        > > The Timer class in the System.Timer namespace will help you. You[/color][/color]
        can[color=blue][color=green]
        > > create it on your UI thread, but the event notifications will come in on
        > > another thread. You can then handle all of your processing in this[/color][/color]
        event[color=blue][color=green]
        > > handler. However, when updating the UI, you will have to use calls to
        > > delegates through an Invoke method on a control in the UI so that the[/color]
        > calls[color=green]
        > > to update the UI are on the UI thread. Otherwise, do all of the[/color]
        > processing[color=green]
        > > on the other thread.[/color]
        >
        > I did try that before my post here and i was facing the same problem.
        > I drop a Timer control in my form, not one of the Winforms controls but[/color]
        the[color=blue]
        > other in the component palette which is a System.Timer control
        > as you say.
        > Nothing changed. I do not know if you mean to create a System.Timer
        > programmaticall y and create and connect an event handler in it,
        > but i think that is the same as the System.Timer control on the form,
        > isn't it ?
        >
        > As with the solution of the hidden secondary form, what is your opinion
        > in that?
        >
        >
        > Thanks a lot for the help!
        >
        > anthonyb
        >
        >[color=green]
        > >
        > > Also, I believe that the event is fired on a thread from the thread
        > > pool. If your processing is long running, then you might want to throw[/color]
        > the[color=green]
        > > processing in another thread (so that you don't tie up the thread pool).[/color]
        >
        > No it isn't a long running process, it is just a try/catch, creating a
        > TcpClient
        > on a apecified computer:port and it last below of 2 seconds.
        >
        >
        >
        >
        >[color=green]
        > >
        > > Hope this helps.
        > >
        > >
        > > --
        > > - Nicholas Paldino [.NET/C# MVP]
        > > - mvp@spam.guard. caspershouse.co m
        > >
        > > "Anthony Boudouvas" <anthonyb@media trel.com> wrote in message
        > > news:OH5v4cqsDH A.2508@TK2MSFTN GP12.phx.gbl...[color=darkred]
        > > > Hi to all,
        > > >
        > > > i have a form with 2 System.Windows. Forms.Timer objects.
        > > > One fire every 5 seconds and the other every 10 seconds,
        > > > the both take actions in two hashtables declared in same form.
        > > >
        > > > When timers fire, main form is somewhat blocking until timers finish
        > > > their job, (socket operations). (Imagine to move the form by it's[/color][/color]
        > caption[color=green][color=darkred]
        > > > bar and it somewhat freeze when timers fire...)
        > > >
        > > > How i can design this so this blocking do not happen ?
        > > >
        > > > Imagine that i am building an application like Sql Server's
        > > > Enterprise Manager, it continuously talks(in the background)
        > > > via sockets so it can display it's registered servers' status but it[/color]
        > > allows[color=darkred]
        > > > you
        > > > to work smoothly with it in the foreground.
        > > >
        > > > I tried to spawn a thread to accomplish that
        > > > (ThreadPool.Que ueUserWorkItem( new[/color][/color][/color]
        WaitCallback(Ch eckPendingServa nts));)[color=blue][color=green][color=darkred]
        > > >
        > > > but the problem remains.
        > > >
        > > > I also thought that because these 2 timer routines take actions on the
        > > > forms' hashtables,
        > > > the code running in these 2 threads will have problems to communicate[/color][/color]
        > with[color=green][color=darkred]
        > > > form's main thread
        > > > and needs some delegate mechanism to do so.
        > > > I also build this too but problem remains...
        > > >
        > > > My last thought is to create a second form with these 2 timers and[/color][/color][/color]
        hide[color=blue][color=green]
        > > it,[color=darkred]
        > > > this form will do all work and just take actions on the -now- public
        > > > hashtables
        > > > on the main form.
        > > >
        > > > Does any other idea comes to mind ?
        > > >
        > > > Thanks a lot
        > > >
        > > >
        > > > anthonyb
        > > >
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Anthony Boudouvas

          #5
          Re: Timer blocking problems...

          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
          message news:Ofa8D7ssDH A.2448@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Anthony,
          >
          > You could always spawn your own thread and then call Sleep on it for[/color]
          the[color=blue]
          > specified amount of time. Does that work for you?[/color]

          Nicholas,

          i have done a fair amount of work with threads, even with calling Invoke on
          the main forms thread and all that stuff.

          try the following code for yourself and you will see what i mean:

          try
          {
          TrySocket = new TcpClient(Serva ntIP, this.ServantLis tenPort);
          }
          catch
          {
          //do something here...
          }


          Even if you spawn this code in a separet thread, you will see that the main
          form is blocking


          anthonyb


          Comment

          Working...