Solved

Should I use a Thread or Timer?

Posted on 2007-12-01
2
2,557 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 85

Accepted Solution

by:
Mike Tomlinson earned 300 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

770 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