Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB Newbie system.threading.timer

Posted on 2011-02-11
11
Medium Priority
?
881 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 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

704 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