Solved

Best way to repeat a method over and over

Posted on 2006-07-03
8
179 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
[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
8 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17029849
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
ID: 17030175
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
ID: 17030430
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 17030926
? should it not be

...
  until Stop;
end;

meikl ;-)
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17031192
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
ID: 17112384
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

740 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