Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Should I use a Thread or Timer?

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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 and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

808 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