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.
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?
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.
Hello,
I am also having same problem, if you got your problem solved running timer in a different thread, please help me.
thanks
S031278
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
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. -
public t1 as threading.thread
-
public t1enabled as boolean=false
-
-
public sub StartCals
-
t1enabled=true
-
t1=new threading.thread(addressof timertick)
-
t1.isbackgroundthread=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.thread.sleep(10000)
-
loop
-
end sub
-
-
public sub UpdateGui
-
-
if me.invokerequired=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(addressof updategui,new object{})
-
-
else
-
'do gui update here
-
textbox1.text=whatever
-
textbox2.text=whatever
-
textbox3.text=whatever
-
-
'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
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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,...
|
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.
|
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
| |
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
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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();...
|
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...
|
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
|
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...
| |