Solved

VB Newbie system.threading.timer

Posted on 2011-02-11
11
838 Views
Last Modified: 2012-06-21
I'm trying to write a VB program to read a "card reader". A person swipes their security badge and it shows the ID number of the badge.

I have the API from the manufacture, I have a button in my app that says read the reader, and it works.

Now I want to not have to hit the button on the app, I want it to continually check the status of the reader and display their ID as they swipe.

I am struggling if this should be a loop or a timer, looks like timer is best. I just can’t figure out the very basics of the timer calling my sub.

Any help would be appreciated.
0
Comment
Question by:TheGrimmReaper
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 14

Assisted Solution

by:quizwedge
quizwedge earned 200 total points
ID: 34875886
In the past, I've just used the basic Timer that you can grab from the toolbox. Then you add the following sub:

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Check for card reader input
End Sub

Open in new window


You may need to add the following line in that Timer1_Tick sub for displaying the output:
Application.DoEvents()

This allows the screen to update while you're in the middle of something (such as a for loop)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34875902
Can you show your existing code that accesses the card reader?
0
 

Author Comment

by:TheGrimmReaper
ID: 34876181
This is my first attempt at VB, I did not know the toolbox had a timer. I will give it a try tomorrow.  Thanks!
Class MainWindow

    Private Sub buttonConnectToReader_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonConnectToReader.Click
        pcProx.usbConnect()
        Dim DeviceID As Long = 0
        Dim rc As Integer = pcProx.usbConnect()

        If rc = 1 Then
            DeviceID = pcProx.GetDID()
            txtStatusBox.Text = "Connected to DeviceID: " & DeviceID.ToString()
        Else
            txtStatusBox.Text = "No devices found to connect with"
        End If
    End Sub

    Private Sub buttonGetActiveID_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonGetActiveID.Click
        Dim Id As [Byte]() = New [Byte](7) {}
        Dim nBits As Integer = pcProx.getActiveID(8)

        If nBits > 0 Then
            Dim s As [String] = ""
            For i As Short = 0 To 1
                Id(i) = pcProx.getActiveID_byte(i)
                s = [String].Format("{0:X2}", Id(i)) & s
            Next
            txtStatusBox.Text = Convert.ToInt32(s, 16)
        Else
            txtStatusBox.Text = "No Card: Click GetActiveID when card is on reader."
        End If
    End Sub


End Class

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34876244
Right...you should be able to do as quizwedge suggests and just place the code from your current buttonGetActiveID_Click() method into the Tick() event of the Timer control.

If there is a noticeable delay when you access the card reader though, then you may want to consider placing this code into a thread instead.

What version VB.Net are you using?  If 2005 (or higher) then you can use the BackgroundWorker() control instead of manual threading.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34876784
Hey buddy. You might want to check read the documentation once again it might let you catch events or register a function address for callback notification. It doesn't seem right that the library would have you poll for the ID. There is a good chance there is a notification that you can subscribe and then call GetID() method. Do you have reference or link to the documentation ?
0
 

Author Comment

by:TheGrimmReaper
ID: 34878751
Attached is the API Manual.

On page 5 its says
"Call one of the GetActiveID functions on a 250 MS timer"

Thanks.
PcProxAPI-7.pdf
0
 

Author Comment

by:TheGrimmReaper
ID: 34878909
I added the timer to my tool box, but it never shows when i open the toolbox in my project.

Maybe I created the wrong type of project? My VB for dummies had me create a WPF application, should i create it in another type?

I'm using Visual Studio 2010 Premium
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 34879010
Be sure to let people know in your question description that you are working with WPF as it is VERY different from a standard WinForms application.

For WPF, you can add a DispatchTimer to your XAML code like this:

Class MainWindow 

    Private WithEvents Timer1 As New System.Windows.Threading.DispatcherTimer

    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Timer1.Interval = TimeSpan.FromSeconds(1)
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Content = DateTime.Now.ToString("h:mm:ss tt")
    End Sub

End Class

Open in new window

0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34879015
Yup. They have you poll the function.  WPF could be a bit much for you instead try using Windows Form Project
0
 

Author Comment

by:TheGrimmReaper
ID: 34879050
Ahhhh. The VB for dummies said WinForms is the old technology, and WPF is the future so to go with WPF.

I created the project in winform and sure enough all of my tool box items showed up. Why do I have no toolbox items in WPF?

If WPF is the future, and if I'm going to learn something new, it should be WPF?

Idle_Mind, I put your timer code in my WPF and it works perfectly! THANKS.

Thanks EVERYONE!

Any other constructive criticism for a newbie before I close this thread???
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34879055
You should also have ToolBox items for WPF...they are just different.

WinForms is going to be around for a long time to come.
WPF will be the standard some day waaaay in the future.

The two are completely different paradigms...  =\
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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