VB.NET: Timer Object - Blocks the Form Thread.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PulkitZery
    New Member
    • Jun 2007
    • 35

    VB.NET: Timer Object - Blocks the Form Thread.

    Hello Everyone,

    I need some help here, I have a form on which I have all the user interface components, and I need to do a big calculation every 5 seconds. I used the Timer object to do this calculation every 5 seconds. So far so good, but my calculation takes about 2-3 seconds and because of that it made my form very unresponsive for that period of time, which is never acceptable.

    I googled the problem and found that If I use System.Timer.Ti mers object It should not block the Form’s thread, because it runs on it own thread. I did that and still same results. After every 5 seconds when my Timer Click happens my form becomes unresponsive for 2-4 seconds. By googling further I found that the SychronizationO bject of the timer is the property that makes sure that the timer synchronizes with that object. I changed that property to a label in my form but that did not help, may be because the label itself is in the same thread of Form.

    Can I make a synchronization object in my code and set the property of my timer to that object? If yes, how can I do that. I no what are other way around to this problem. Can anyone help me in figuring this out?

    By the way this is my Timer’s Properties:
    AutoReset: True
    SychronizationO bject: myForm

    Thanks in advance for your help.

    Jerry.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    While it may not be the problem, you do realize that processors aren't magical devices. Just because a heavy calculation is moved to another thread, does not mean it doesn't still eat resources.

    With that being said however, I've never had trouble putting computation heavy functions in another thread and had my gui function properly.

    HAve you tried various uses of the Update() function all Control objects have to force them to gain process time?

    Comment

    • PulkitZery
      New Member
      • Jun 2007
      • 35

      #3
      Originally posted by Plater
      While it may not be the problem, you do realize that processors aren't magical devices. Just because a heavy calculation is moved to another thread, does not mean it doesn't still eat resources.

      With that being said however, I've never had trouble putting computation heavy functions in another thread and had my gui function properly.

      HAve you tried various uses of the Update() function all Control objects have to force them to gain process time?
      Hello, Thanks for the reply,

      Well, I was trying to simplify the topic. In actually what I am doing is not a calculation, I am calling a method using .net remoting class. What happens is my program checks to see if the connection is available every 5 seconds or so. But if the remote server does not respond, the client program freezes right there until the remote method call times out. And that is what I am looking to resolve. I don’t want my User Interface become unresponsive for that period of time. I want this remote call on different thread, and I need to use Timer object so that I can check the connection every 5 second.

      The only part I am looking to resolve is to run this time thread not on the same Thread on which form is running, may be by creating a synchronize object or may be by any other way out there. Hope this clears you that its not just a big calculation, it’s a method call that freezes the user interface, which makes my Form Unresponsive until the method call times out.

      Comment

      • s031278
        New Member
        • Aug 2007
        • 1

        #4
        Hello,

        I am also having same problem, if you got your problem solved running timer in a different thread, please help me.

        thanks

        S031278

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          The System.Timers.T imer should handle what you want.
          If not, try the System.Windows. Forms.Timer object

          Comment

          • rlange
            New Member
            • Jul 2008
            • 1

            #6
            This code is hand typed, has some errors, not tested, no error trapping, but will get you on the correct path.

            if the timer tick needs to update the GUI then you will need to invoke a sub on the gui thread to do the GUI update. I would still do the long cacls on the 2nd thread, and only when its time to do the GUI update would I invoke another sub to handle that on the gui thread. This will allow all intensive calcs to be performed on the 2nd thread, and only make GUI updates on the GUI thread. This can be added to a form, and just call startcacls to get the ball rolling.


            [code=vbnet]
            public t1 as threading.threa d
            public t1enabled as boolean=false

            public sub StartCals
            t1enabled=true
            t1=new threading.threa d(addressof timertick)
            t1.isbackground thread=true
            t1.start
            end sub

            public sub StopCacls
            t1enabled=false
            t1.abort()
            end sub

            public sub timertick
            while t1enabled=true

            'do long calcs here

            'call the update gui sub from the 2nd thread
            UpdateGui

            'sleeping 2nd thread specified internal
            threading.threa d.sleep(10000)
            loop
            end sub

            public sub UpdateGui

            if me.invokerequir ed=true then

            'Call updategui on main GUI thread
            'if you need to pass parameters then you should use global shared vars, or
            'you can make a parameter array and create a delegate sub to match the sub signature and invoke that. me.invokereq tests to see if this code is currently running on the GUI thread. invokereq is true if the sub is called from antoher thread, and false if it is on the GUI thread.

            'invoking this sub on the GUI thread
            me.invoke(addre ssof updategui,new object{})

            else
            'do gui update here
            textbox1.text=w hatever
            textbox2.text=w hatever
            textbox3.text=w hatever

            'basically the updategui function is called from 2nd thread in the loop above
            'me.invokereq will be equal to true when the sub is called from the 2ndthread.
            'this will make the me.invoke fire, which just calls this sub again, but on the gui
            'thread. When this sub is called from the GUI thread me.invokereq will equal '
            'false and the else half of this if statement will run on the gui thread, which is
            'where you put the code to do the gui updates.


            end if

            end sub
            [/code]

            Comment

            Working...