Link to home
Start Free TrialLog in
Avatar of TheGrimmReaper
TheGrimmReaper

asked on

VB Newbie system.threading.timer

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.
SOLUTION
Avatar of quizwedge
quizwedge
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
Can you show your existing code that accesses the card reader?
Avatar of TheGrimmReaper
TheGrimmReaper

ASKER

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

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.
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 ?
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
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yup. They have you poll the function.  WPF could be a bit much for you instead try using Windows Form Project
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???
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...  =\