473,659 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

35 New Member
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.
Jul 16 '07 #1
5 7476
Plater
7,872 Recognized Expert Expert
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?
Jul 16 '07 #2
PulkitZery
35 New Member
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.
Jul 16 '07 #3
s031278
1 New Member
Hello,

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

thanks

S031278
Aug 2 '07 #4
Plater
7,872 Recognized Expert Expert
The System.Timers.T imer should handle what you want.
If not, try the System.Windows. Forms.Timer object
Aug 2 '07 #5
rlange
1 New Member
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.


Expand|Select|Wrap|Line Numbers
  1. public t1 as threading.thread
  2. public t1enabled as boolean=false
  3.  
  4. public sub StartCals
  5. t1enabled=true
  6. t1=new threading.thread(addressof timertick)
  7. t1.isbackgroundthread=true
  8. t1.start
  9. end sub
  10.  
  11. public sub StopCacls
  12. t1enabled=false
  13. t1.abort()
  14. end sub
  15.  
  16. public sub timertick
  17. while t1enabled=true
  18.  
  19. 'do long calcs here
  20.  
  21. 'call the update gui sub from the 2nd thread
  22. UpdateGui
  23.  
  24. 'sleeping 2nd thread specified internal
  25. threading.thread.sleep(10000)
  26. loop
  27. end sub
  28.  
  29. public sub UpdateGui
  30.  
  31. if me.invokerequired=true then
  32.  
  33. 'Call updategui on main GUI thread
  34. 'if you need to pass parameters then you should use global shared vars, or
  35. '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.
  36.  
  37. 'invoking this sub on the GUI thread
  38. me.invoke(addressof updategui,new object{})
  39.  
  40. else
  41. 'do gui update here
  42. textbox1.text=whatever
  43. textbox2.text=whatever
  44. textbox3.text=whatever
  45.  
  46. 'basically the updategui function is called from 2nd thread in the loop above
  47. 'me.invokereq will be equal to true when the sub is called from the 2ndthread.
  48. 'this will make the me.invoke fire, which just calls this sub again, but on the gui 
  49. 'thread. When this sub is called from the GUI thread me.invokereq will  equal '
  50. 'false and the else half of this if statement will run on the gui thread, which is 
  51. 'where you put the code to do the gui updates.  
  52.  
  53.  
  54. end if
  55.  
  56. end sub
  57.  
Jul 26 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

77
4571
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the process starts is not especially important, but it must be complete within a small number of seconds. The operations I am performing do not take a long time (hundreds of milliseconds), but as each part of the process is complete, my worker thread...
5
2527
by: Saga | last post by:
Hi all, I just read the thread by the same name posted on Jun 15, 9:45 AM, but I am looking at it from a different point of view. I came across this question in terms of functionality. Are VB and C# (.NET) really functionally equivalent? I had read that although they are very similar, C# does extend functionality further than VB.NET; however, when I searched for pages regarding this, I could not find any.
6
2865
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things, periodically calls a method in this TimerState object which raises an event to the startup form,...
5
2061
by: Leon | last post by:
Is there a way I can create a thread at application level that running all the time along with application exists? I have tried to do the above thing, and I found for some reason, the thread only can be excuted once after it is created. Thanks very much for your help.
12
4131
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. However in vb.net you get continual messages (even setting the system modal property). Firstly, are these two assumptions right and if so what is the approved
2
6948
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
0
3659
by: shamirza | last post by:
· When was .NET announced? Bill Gates delivered a keynote at Forum 2000, held June 22, 2000, outlining the .NET 'vision'. The July 2000 PDC had a number of sessions on .NET technology, and delegates were given CDs containing a pre-release version of the .NET framework/SDK and Visual Studio.NET. · When was the first version of .NET released? The final version of the 1.0 SDK and runtime was made publicly available around 6pm PST on...
0
3832
by: shamirza | last post by:
· What is view state and use of it? The current property settings of an ASP.NET page and those of any ASP.NET server controls contained within the page. ASP.NET can detect when a form is requested for the first time versus when the form is posted (sent to the server), which allows you to program accordingly. · What are user controls and custom controls? Custom controls: A control authored by a user or a third-party software vendor that...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.