Solved

Best way to repeat a method over and over

Posted on 2006-07-03
8
165 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

23 Experts available now in Live!

Get 1:1 Help Now