Solved

Best way to repeat a method over and over

Posted on 2006-07-03
8
159 Views
Last Modified: 2010-04-05
Hi,
   I have a method that checks if there is a smartcard inserted into the card-reader, and reads the data on the card and stores it into a database if it is inserted, otherwise it just exits the method.

The method looks as follows:
--------------------------------------------------------------------------------------------------------------------------------------------------
SmartC := CreateFile(PChar('COM' + IntToStr(1)), GENERIC_READ + GENERIC_WRITE, 0, nil, OPEN_EXISTING, 0, 0);

//Setup connection

If Card Inserted
   //Read Data
   //Write to database
else
  //Exit method

CloseHandle(SmartC);
--------------------------------------------------------------------------------------------------------------------------------------------------

What would be the best way to repeat this method.
I have put this method into a timer, and I get the result i'm after, but I think this would waste a lot of the CPU's processing power, considering the method is around 75 lines.

I'm using Delphi 7, and Win-XP

Regards,
Chris
0
Comment
Question by:ChrisLM
8 Comments
 
LVL 10

Expert Comment

by:atul_parmar
Comment Utility
Var
  Stop : Boolean;
begin
  Stop := False;
end
procedure MethodName;
begin
  repeat  
  //your logic
  Application.ProcessMessage;
  until Not Stop;
end;
0
 
LVL 11

Expert Comment

by:calinutz
Comment Utility
Also you could use a separate thread for doing the checking.
Do a search on Delphi's help, or even internet (google) for delphi threading and you'll find a lot of examples

Regards
0
 
LVL 11

Expert Comment

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

 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
? should it not be

...
  until Stop;
end;

meikl ;-)
0
 
LVL 10

Expert Comment

by:atul_parmar
Comment Utility
Thanks meikl.

Var
  Stop : Boolean;
begin
  Stop := False;
end
procedure MethodName;
begin
  repeat  
  //your logic
  Application.ProcessMessage;
  until Stop;
end;
0
 
LVL 4

Accepted Solution

by:
Delphian earned 500 total points
Comment Utility
I don't like the busy wait idea of atul_parmar... It'll eat much more cpu (if I got it right)...
Your timer approach seems very reasonable, just set the timer to about 1/2 of second or a little more.

Anyway, giving a look at your method skeleton, seems that real processing really begins after the Card_Inserted flag is true. If so, you can simplify it to just look if the card is inserted and only setup the connection, read data and write to database after the detection of card_inserted... It will detect the card and just do the dirty work after the flag.

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

11 Experts available now in Live!

Get 1:1 Help Now