Solved

How to perform regular screen refresh while processing?

Posted on 2008-10-26
16
2,127 Views
Last Modified: 2012-05-05
Hi

my application is processing data and it can take up to 10 minutes to do. It can make tens of millions of cycles... to have some sort of screen refresh and indicator I put this to every main procedure :

if counter mod 10000 then
begin
    StatusForm.Show;
    StatusForm.myLabel.Caption := 'Procesing #'+myIndicatorNo;
    Application.ProcessMessages;
end;


Now, this works OK. Obivously with 'IF' I make sure that refresh doesn't take too much of resources, because it CAN, at least in my application!

Now, I would like to have another, easier and much less code approach. So I was thinking of having a Timer that would do the same think every second.

Is this possible? Can Timer take a lot of resources, while waiting for 1 sec to pass? Is this, being executed on Timer event, actually possible in one-threaded application?

If this is possible for my application, please give me some pointers.

Thank you

0
Comment
Question by:Delphi_developer
[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
  • 4
  • 3
  • +1
16 Comments
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 22808545
I would have thought that Timer is interrupt driven, and therefore uses less resources, but others here will have a clearer idea.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22808700
nope, not in 1 threaded app

the timer event will be put on the queue, and wait for the thread to process it
which off course will never happen as your calculation routine is using the 1 thread

you really need to put the calculation routine in a thread
if you want the GUI not to freeze
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 22808733
I for one would be interested to know how to implement that
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 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 22810346
well if is only 1 routine just put it in the
procedure Execute of a thread

I even wrote a Thread type for this, from which you can callback the progress

type
 
  TCallbackProc = procedure (aMessage: string; aMessageInfo: Integer = 0) of object;
 
  TCallbackThread = class(TThread)
  private
    FCallBack: TCallbackProc;
    FCallbackMsg: string;
    FCallbackMsgInfo: integer;
    procedure SynchedCallback;
  protected
    procedure DoCallback(aMsg: string; aMsgInfo: integer = 0); virtual;
    property Callback: TCallbackProc read FCallback;
  public
    constructor Create(aCallback: TCallbackProc; CreateSuspended: Boolean = False); reintroduce; virtual;
  end;
 
{ TCallbackThread }
 
constructor TCallbackThread.Create(aCallback: TCallbackProc; CreateSuspended: Boolean = False);
begin
  inherited Create(CreateSuspended);
  FreeOnTerminate := True;
  FCallback := aCallback;
end;
 
procedure TCallbackThread.DoCallback(aMsg: string; aMsgInfo: Integer = 0);
begin
  FCallbackMsg := aMsg;
  FCallbackMsgInfo := aMsgInfo;
  Synchronize(SynchedCallback);
end;
 
procedure TCallbackThread.SynchedCallback;
begin
  if Assigned(FCallback) then
    FCallBack(FCallbackMsg, FCallbackMsgInfo);
end;

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22810347
Example of DB reading records

procedure TMyThread.Execute;
var I: Integer;
begin
  DoCallback('Thread started', 0);
 
  DoCallback('Records ' + IntToStr(Query.RecordCount)), 1);
  I := 0;
  while not Query.Eof do 
  begin
    Inc(I);
    DoCallback('Handling record ' + IntToStr(I), 2);
    Query.Next;
  end;
 
  DoCallback('Thread started', 0);
end;

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22810366
in your Form create a thread with the callback proc assigned


type
  TForm1 = class(TForm)
    ProgressBar1: TProgressBar;
    Panel1:  TPanel;
  private
    fThread: TMyThread;
    procedure ProgressUpdate(aMessage: string; aMessageInfo: Integer = 0);
  end;
 
procedure TForm1.ProgressUpdate(aMessage: string; aMessageInfo: Integer = 0);
begin
  Panel1.Caption := aMessage;
  case aMessageInfo of
    0: ProgressBar1.Position := 0;
    1: 
    begin
      ProgressBar1.Position := 0;
      ProgressBar1.Max := StrToInt(Copy(aMessage, 8, Length(aMessage)));
    end;
    2: ProgressBar1.Position := ProgressBar1.Position + 1;
    3: 
    begin
      ProgressBar1.Position := ProgressBar1.Max;
      fThread := nil;
    end;
  end;
end;
 
procedure TForm1.Button1Click(Sender: Object);
begin
  if not Assigned(fThread) then 
    fThread := TMyThread.Create(ProgressUpdate);
end;

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22810388
o yes,
no need to call
Application.ProcessMessages
0
 
LVL 31

Expert Comment

by:moorhouselondon
ID: 22810626
GG:

v. comprehensive ty.  I hope it answers DD's q!
0
 

Author Comment

by:Delphi_developer
ID: 22829566
@Geert Gruwez: Thank you! I will need some time to test if this can be applied to my app...
0
 

Author Closing Comment

by:Delphi_developer
ID: 31514375
I decided that running my app in threads is a little too complicated... too many errors and too much things still to learn about threads, for now.

But thank you for the solution, I will definately use it in another app.

I will go back to  Application.ProcessMessages; for now, it is the easiset way and it works 100% for my app.

Thank you
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22907584
Please read the Help about giving grades for answers.
If someone gives you an excellent approach then the answer deserves an A
The questioner deserves a B for not implementing it.
And not vice versa
0
 

Author Comment

by:Delphi_developer
ID: 22907626
Sorry, I thought C is for not using the proposed solution and A for it to be a solution to the question. So, I gave B because it is a good solutions, but not gonna use it as a solutinos for my question... I see I messed up.

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22909583
nobody's perfect :)
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22973718
>>But thank you for the solution, I will definately use it in another app.

don't you think you should change the grade to an A since you are going to use the solution ?
0
 
LVL 1

Expert Comment

by:Vee_Mod
ID: 22974940
Grade change by Asker request.

Vee_Mod
Experts Exchange Moderator
0
 

Author Comment

by:Delphi_developer
ID: 22974960
@Geert_Gruwez: thank you for pointing this out - I didn't know I can do this, until your comment and a quick browse through the Help

@Vee_Mod: Thank you.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Can Viruses spread while transferring Binary data with Winsock 2 105
select query - oracle 16 103
Dynamically Created Query 3 78
Delphi: barcode reading on android platform 1 98
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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