?
Solved

Should I use a Thread or Timer?

Posted on 2007-12-01
2
Medium Priority
?
2,585 Views
Last Modified: 2013-11-06
It was my understanding that a Timer is explicitly executed in it's own thread. Is this correct? I need to have a full time monitor in the background of my application that monitors possibly machine faults and inputs. I also read on this forum(EE) that A Timer will require attention by your program before it can execute any other code. In this case, Would a background thread be best for my application? Also, I need to constantly update  variables by sending a command using an ActiveX control to a PCI card. After I send the request to the card, it responds (<.1 ms). I need to constantly check the inputs for changes, so i would need to throw a timer on the Send command to the PCI Card. Is this a good way of going about it (Timer inside an explicitly created Thread), or is there another recommendation.

Also, I have been trying to create a Timer in a Module, but I cannot seem to have any luck with the "Tick" event. I do the following:


Module 1

Public Sub FaultMonitor()

Dim FaultTimer as New System.Timer

FaultTimer.Interval = 200
FaultTimer.Enabled = True

End Sub

End Module


How do I execute code when the timer ticks? I cannot seem to get it working, and I have been trying to base my code off the Timer tick events that I have on my forms, without any luck.


P.S. I asked a question similar to this before, but Now that I have researched more, I am more confused :/. Sorry if this is viewed as a re-post, I am just trying to understand how it all works.



Thanks!
0
Comment
Question by:Gssc1414
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1200 total points
ID: 20389665
"It was my understanding that a Timer is explicitly executed in it's own thread."

Depends on the Timer being used!  The System.Windows.Forms.Timer runs in the SAME thread as the main UI.  The other Timers run in their OWN threads.

"Also, I have been trying to create a Timer in a Module, but I cannot seem to have any luck with the "Tick" event."

Use WithEvents/Handles (or AddHandler) to wire up a sub:
Module Module1
 
    Private WithEvents FaultTimer As System.Timers.Timer
 
    Public Sub FaultMonitor()
        FaultTimer = New System.Timers.Timer
        FaultTimer.Interval = 200
        FaultTimer.Enabled = True
    End Sub
 
    Private Sub FaultTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles FaultTimer.Elapsed
        ' do something here....
    End Sub
 
End Module

Open in new window

0
 
LVL 3

Author Comment

by:Gssc1414
ID: 20400728
Perfect.  Thank you, this makes more sense now!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question