Solved

VB Newbie system.threading.timer

Posted on 2011-02-11
11
865 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
[X]
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
  • 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 86

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 86

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 86

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 86

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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