Solved

Should I use a Thread or Timer?

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 72
System32Int Error 8 44
Limiting string to two deciamls 18 34
Open a word document 23 18
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 use and create new code templates in NetBeans IDE 8.0 for Windows.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now