Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to Stop Hanging VB.net Application Software whenever i am using Timer


actually my code is every 10 Sec Software Auto Check and Email to Client but problem is whenever timer starts every time form can accessible by user, every time software is hanging please help me how can i need to work as backgroundly because my email code get data from customer database and after it send auto as per email address.


Please Help
Posted
Comments
sohail awr 22-Mar-14 12:27pm    
First you should give your sample code where you have used the timer. After I will be able give exact solution.
hareshdgr8 24-Mar-14 2:36am    
Private Sub frmMail_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
tm_Email.Interval = 600000
tm_Email.Start()
End Sub
Private Sub tm_Email_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tm_Email.Tick
If IsConnectionAvailable() = True Then
sendMail()
else
tm_Email.Interval = 600000
End if


End Sub
private sub sendMail()
' Mail Code by CLient wise
End sub

Here is my Code please help me

1 solution

It sounds like you're using the Timer that's in the Toolbox. This is a timer that depends on Events in order to notify your application of the Tick. This also means it runs entirely on the UI thread (the thread your application started up on). When the thread is running your check every 10 seconds, it can't do other things, like update the UI (repaint it) and respond to user clicks.

The only solution to that is to move your checking code to a background thread. That is easily accomplished by removing the Timer you dropped on the form and instead use the System.Threading.Timer[^] class. It's callback will use a ThreadPool thread to execute your check code.
 
Share this answer
 
Comments
hareshdgr8 24-Mar-14 4:43am    
how can i use please give me correct example if my code is as follows...

Private Sub frmMail_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
tm_Email.Interval = 600000
tm_Email.Start()
End Sub
Private Sub tm_Email_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tm_Email.Tick
If IsConnectionAvailable() = True Then
sendMail()
else
tm_Email.Interval = 600000
End if


End Sub
private sub sendMail()
' Mail Code by CLient wise
End sub

Here is my Code please help me
Dave Kreskowiak 24-Mar-14 9:37am    
There are tons of examples using the Threading.Timer class all over the web. I'm not going to rewrite your code for you.

https://www.google.com/#q=system.threading.timer+example
sohail awr 25-Mar-14 14:04pm    
You can use 2 timers 1st will check the IsConnectionAvailable() and 2nd will send the mails.

In your function IsConnectionAvailable()

Use condition

If IsConnectionAvailable() = True then
Timer2.Start()
else
Timer2.Stop()
End if

On Timer1 Tick event

Call IsConnectionAvailable()

On Timer2 Tick Event

Call Sendmail()

Also remember the Timer interval you should define in timer properties not on page load event.
Dave Kreskowiak 25-Mar-14 15:03pm    
The problem with your solution is that it doesn't solve the problem. You're still using the Forms Timer in the Toolbox and are still running "long running" code on the UI thread, blocking it from being responsive to what the user is clicking on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900