Solved

VB Newbie system.threading.timer

Posted on 2011-02-11
11
834 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
Comment Utility
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
Comment Utility
Can you show your existing code that accesses the card reader?
0
 

Author Comment

by:TheGrimmReaper
Comment Utility
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:TheGrimmReaper
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Custom auto number 6 51
Select all rows in datagridview 15 18
Tabcontrol Caption? 1 21
Clone Visual studio 2013 solution 5 15
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now