Solved

Should I use a Thread or Timer?

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

696 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