Timer control to flash message

pjslevin

Member
Joined
Oct 28, 2007
Messages
10
Programming Experience
10+
I have added a timer control to a VB.NET windows form to have a message flash on and off between ticks. When I load the form I have the timer disabled. When i click on a command button, I set Timer1.Enabled to True so that the timer can begin flashing the message. The timer does not flash the message. I added a MsgBox function after where I turn on the timer. When the message box is displayed and I click OK, then the timer starts flashing the message. I turn the timer off later and that seems to work fine. I have tried the Timer1.Start() method but that does not work. Any thoughts on why the message box allows it to run but the form does not? Thanks, Pat
 
Hello

I gave a quick shot to your problem. I set my timer interval to 100
Here is timer tick method :

VB.NET:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Visible = Not Label1.Visible
    End Sub

Here it is flashing.

If you can copy & paste your code, maybe we may help better.

Thx
 
The Timer1_Tick part works OK. I want to have the message disabled until the user clicks on a command button. Below is my code for this. I enable the timer. I do not want to display a message box, but this is the only way I could get the timer to activate. Then, I call some functions to do some stuff. When the functions return to the calling function listed here, I want to disable the timer again. It does not work the way I want it to unless I dispaly a message box first.


' turn on PROCESSING message
Timer1.Enabled = True

If Timer1.Enabled = True Then
MsgBox("timer is on", MsgBoxStyle.Information, "timer")
Else
MsgBox("timer is off", MsgBoxStyle.Information, "timer")
End If

Companies = RetrieveCompanies()
QueryDB(Companies)

'turn off PROCESSING message
Timer1.Enabled = False
lblMessage.Visible = False
 
I had some weird stuff with timers too, if the timer does not start with the .enabled = true, have you tried to use the timer1.Start method, you should not have to have a msgBox open to get it to work. Also is your timer enabled(true) in the designer?
 
I have Enabled in my designer set to True. When I start the program I do not want the message to appear so in Form_Load I have .Enabled = False. I have tried .Start() but that does not work, either. I'm not sure where to go from here.
 
' turn on PROCESSING message
Timer1.Enabled = True

If Timer1.Enabled = True Then
MsgBox("timer is on", MsgBoxStyle.Information, "timer")
Else
MsgBox("timer is off", MsgBoxStyle.Information, "timer")
End If

Companies = RetrieveCompanies()
QueryDB(Companies)

'turn off PROCESSING message
Timer1.Enabled = False
lblMessage.Visible = False

As far as I understand, you're trying to display a flashing "Please Wait" message ( or something similiar ) while you do the DB stuf.

So try this :
Put a button on the form and on click event :

VB.NET:
     ' turn on PROCESSING message       
        Timer1.Enabled = True
        
        Companies = RetrieveCompanies()
        QueryDB(Companies)

        'turn off PROCESSING message
        Timer1.Enabled = False
        lblMessage.Visible = False

Also add the following code to timer tick event :

VB.NET:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblMessage.Visible = Not Label1.Visible
    End Sub

But It may be that your db query doesn't let timer to fire tick event properly.

Try the code once with out DB query, and let us know the result.
 
How about moving the timer code to a Sub routine that fires only when you want it too. And you may need to use Application.DoEvents after the .start().
 
I already have the code I posted in a command click event. I tried commenting out QueryDB but that did not help. What is the Application.DoEvents method?
 
You're probably doing your processing work on the UI thread, which means it's not available to process the timer tick calls, or to flash the message

Solution: DOn't do your long running ops on the thread that is intended to process the windowing messages..

And don't use DoEvents either; not only will it not work when your thread is off waiting for some long running external process to complete, but it's dumb. Use a BackgroundWorker Class (System.ComponentModel) to do the long op, which will leave your UI thread free to draw the UI and respond to user input (avoids the "Not Responding" messages)
 
Back
Top