Board index » delphi » Updating Mainform caption from thread will clear caption

Updating Mainform caption from thread will clear caption

Hello,

I am updating Mainform caption (to show progress) from thread (I am using
CriticalSection to avoid AV's). When I update caption it will be totally
cleared. Everything will disappear. (Panel caption will be updated just as
it should)

Code is following:

procedure TMyThread.ShowFileName(const AFName: string; AIndex, ACount:
Integer);
var
  Tmp: string;
begin
  Tmp := Format('%.2f', [(AIndex / ACount) * 100]) + '%';
  FCriticalSection.Enter;
  try
    frmMain.Caption := Tmp + ' ' + FFormCaption;
    frmMain.pnlProgress.Caption := '    (' + Tmp + ') ' + AFName;
  finally
    FCriticalSection.Leave;
  end;
  frmMain.Refresh;
  frmMain.pnlProgress.Refresh;
  Application.ProcessMessages;
end;

Any Ideas??

 

Re:Updating Mainform caption from thread will clear caption


Quote
Tommi Prami wrote:

> Hello,

> I am updating Mainform caption (to show progress) from thread (I am using
> CriticalSection to avoid AV's). When I update caption it will be totally
> cleared. Everything will disappear. (Panel caption will be updated just as
> it should)

> Code is following:

> procedure TMyThread.ShowFileName(const AFName: string; AIndex, ACount:
> Integer);
> var
>   Tmp: string;
> begin
>   Tmp := Format('%.2f', [(AIndex / ACount) * 100]) + '%';
>   FCriticalSection.Enter;
>   try
>     frmMain.Caption := Tmp + ' ' + FFormCaption;
>     frmMain.pnlProgress.Caption := '    (' + Tmp + ') ' + AFName;
>   finally
>     FCriticalSection.Leave;
>   end;
>   frmMain.Refresh;
>   frmMain.pnlProgress.Refresh;
>   Application.ProcessMessages;
> end;

> Any Ideas??

Try this :  (not tested)

 procedure TMyThread.ShowFileName(const AFName: string; AIndex, ACount:
 Integer);
 var
   Tmp: string;
 begin
   Tmp := Format('%.2f', [(AIndex / ACount) * 100]) + '%';
   FCriticalSection.Enter;
   try
     frmMain.Caption := Tmp + ' ' + FFormCaption;
     frmMain.Update;    
     frmMain.pnlProgress.Caption := '    (' + Tmp + ') ' + AFName;
     frmMain.pnlProgress.Update;
   finally
     FCriticalSection.Leave;
   end;
 end;

Hope it helps...

--
Guido Geurts
System Engineer

www.ADTechno.com
Guido.Geu...@adtechno.be
Tel : +32 (0)11 30 13 74

Never knock on Death's door. Ring his doorbell and run.  (He hates
that.)

Re:Updating Mainform caption from thread will clear caption


Sorry,

don't work, damnation...

-Tommi-

Quote
"Guido Geurts" <Guido.Geu...@adtechno.be> wrote in message

news:3BD5471C.29D97DBD@adtechno.be...
Quote
> Tommi Prami wrote:

> > Hello,

> > I am updating Mainform caption (to show progress) from thread (I am
using
> > CriticalSection to avoid AV's). When I update caption it will be totally
> > cleared. Everything will disappear. (Panel caption will be updated just
as
> > it should)

> > Code is following:

> > procedure TMyThread.ShowFileName(const AFName: string; AIndex, ACount:
> > Integer);
> > var
> >   Tmp: string;
> > begin
> >   Tmp := Format('%.2f', [(AIndex / ACount) * 100]) + '%';
> >   FCriticalSection.Enter;
> >   try
> >     frmMain.Caption := Tmp + ' ' + FFormCaption;
> >     frmMain.pnlProgress.Caption := '    (' + Tmp + ') ' + AFName;
> >   finally
> >     FCriticalSection.Leave;
> >   end;
> >   frmMain.Refresh;
> >   frmMain.pnlProgress.Refresh;
> >   Application.ProcessMessages;
> > end;

> > Any Ideas??

> Try this :  (not tested)

>  procedure TMyThread.ShowFileName(const AFName: string; AIndex, ACount:
>  Integer);
>  var
>    Tmp: string;
>  begin
>    Tmp := Format('%.2f', [(AIndex / ACount) * 100]) + '%';
>    FCriticalSection.Enter;
>    try
>      frmMain.Caption := Tmp + ' ' + FFormCaption;
>      frmMain.Update;
>      frmMain.pnlProgress.Caption := '    (' + Tmp + ') ' + AFName;
>      frmMain.pnlProgress.Update;
>    finally
>      FCriticalSection.Leave;
>    end;
>  end;

> Hope it helps...

> --
> Guido Geurts
> System Engineer

> www.ADTechno.com
> Guido.Geu...@adtechno.be
> Tel : +32 (0)11 30 13 74

> Never knock on Death's door. Ring his doorbell and run.  (He hates
> that.)

Re:Updating Mainform caption from thread will clear caption


I am afraid I am out of ideas now...
Maybe someone else can kick in here ?

Quote
Tommi Prami wrote:

> Sorry,

> don't work, damnation...

> "Guido Geurts" <Guido.Geu...@adtechno.be> wrote in message

> > Try this :  (not tested)

> >  procedure TMyThread.ShowFileName(const AFName: string; AIndex, ACount:
> >  Integer);
> >  var
> >    Tmp: string;
> >  begin
> >    Tmp := Format('%.2f', [(AIndex / ACount) * 100]) + '%';
> >    FCriticalSection.Enter;
> >    try
> >      frmMain.Caption := Tmp + ' ' + FFormCaption;
> >      frmMain.Update;
> >      frmMain.pnlProgress.Caption := '    (' + Tmp + ') ' + AFName;
> >      frmMain.pnlProgress.Update;
> >    finally
> >      FCriticalSection.Leave;
> >    end;
> >  end;

--
Guido Geurts
System Engineer

www.ADTechno.com
Guido.Geu...@adtechno.be
Tel : +32 (0)11 30 13 74

Never knock on Death's door. Ring his doorbell and run.  (He hates
that.)

Re:Updating Mainform caption from thread will clear caption


1st thing: I don't see how your Critical Section helps. Where
in the context of the main thread do you also use it?

2nd thing: frmMain as well as the Application variable
belong to the main thread and should not be accessed at all
from a secondary thread. Do avoid this, have a look at the
Synchronize procedure. What does the main thread do at the
moment? If it does nothing, and you use the thread just to
not "lock" the application, there should be no problems with
synchronizing the update of the form.

// David

Re:Updating Mainform caption from thread will clear caption


Quote
In article <3bd53471$1_2@dnews>, Tommi Prami wrote:
> I am updating Mainform caption (to show progress) from thread (I am using
> CriticalSection to avoid AV's).

That is not the way to do it. You either use Synchronize or send a
WM_SETTEXT message to the main form, both make sure that the code
will modify the caption in the context of the main thread only.
The VCL is not thread-safe, you simply cannot access forms and stuff from
secondary threads safely.

Quote
> procedure TMyThread.ShowFileName(const AFName: string; AIndex, ACount:
> Integer);
> var
>   Tmp: string;
> begin
>   Tmp := Format('%.2f', [(AIndex / ACount) * 100]) + '%';

    SendMessage( frmMain.handle, WM_SETTEXT, 0,
                 lparam( Pchar( tmp ));

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:Updating Mainform caption from thread will clear caption


Quote
> That is not the way to do it. You either use Synchronize or send a
> WM_SETTEXT message to the main form, both make sure that the code
> will modify the caption in the context of the main thread only.
> The VCL is not thread-safe, you simply cannot access forms and stuff from
> secondary threads safely.

Should I add public propertyes for main form and update captions with
timer?? or is there some better way to do that.

-Tommi-

Other Threads