🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #16184

Re: TThread with messages

#16184
From: Lapo
Date: Sun, 25 May 2003 22:28
151 lines
5120 bytes
Okay, I'll post the code at the end of  the message.
I see another problem that is more important for me than the fast update.
Using "while" loops the CPU is busy at 99% (checked using TaskManager) and
all the other applications work like a snail.
Any suggestions?
Maybe I should use messages also to solve  this problem?

I hope the code is clear, it's my first attempt with thread.

Thanks for your help.

Lapo

//--------------------------------------------------------------------------
-
class TPlayThread : public TThread {
  private:
    TImgView32 *Frame;
    TColor Color;
    TForm *Sheet;
    TJPEGImage *JPG;
    int FrameIndex;
    TTrackBar *Velocity;
    TScrollBar *ScrollBar;
    int Pause;
    TLabel *Status;
    AnsiString Error;
    int Offset;
    int Frames;
    int Fps;

    AnsiString FolderName;
    TStringList *FrameList;

    AnsiString Current;

  protected:
    void __fastcall Execute();
    void __fastcall UpdateFrame(void);
    void __fastcall DeleteFrame(void);
  public:
    int Step;
    int Last;
    __fastcall TPlayThread(TDeviceItem *DeviceItem, TLabel *AStatus,
TTrackBar *AVelocity, TForm *ASheet, TImgView32 *AFrame, TScrollBar
*AScrollBar);
};
//--------------------------------------------------------------------------
-
__fastcall TPlayThread::TPlayThread(TDeviceItem *DeviceItem, TLabel
*AStatus, TTrackBar *AVelocity, TForm *ASheet, TImgView32 *AFrame,
TScrollBar *AScrollBar)
        : TThread(true)
{
  FreeOnTerminate=true;
  Sheet=ASheet;
  Velocity=AVelocity;
  Status=AStatus;
  Error="";
  Frame=AFrame;
  FrameIndex=0;
  Frames=DeviceItem->Frames;
  Fps=DeviceItem->Fps;
  Offset=DeviceItem->Offset;
  SFSFileName=DeviceItem->PathName;
  FolderName=DeviceItem->FolderName;
  ScrollBar=AScrollBar;
  FrameList=new TStringList();
  FrameList->Sorted=true;

// Populate the StringList with the filenames of the frames (*.jpg)
  TSearchRec SR;
  if(FindFirst("\\"+FolderName+"\\*.jpg", faAnyFile, SR)==0) {
    do {
        FrameList->Add("\\"+FolderName+"\\"+SR.Name);
    } while(FindNext(SR)==0);
    FindClose(SR);
  }
// Initialize the ScrollBar to indicate the current position
  ScrollBar->Max=FrameList->Count-1;
  ScrollBar->Position=0;

// Initialize the frames count
  Last=FrameList->Count;
  Step=1;
}
//--------------------------------------------------------------------------
-
void __fastcall TPlayThread::Execute()
{
  try {
    JPG=new TJPEGImage();

// Start with the first frame
    FrameIndex=0;
    while(!Terminated) {
      while(Step!=0) {
// Load the frame
        Current=FrameList->Strings[FrameIndex];
        JPG->LoadFromFile(Current);
// Display it
        Synchronize(UpdateFrame);
// Wait for the next
        Sleep(Pause);
// Goto next frame
        FrameIndex+=Step;
// Check for begin reached...
        if(FrameIndex<0) {
          FrameIndex=0;
          Step=0;
        }
// ...or end reached
        else if(FrameIndex==Last) {
          FrameIndex=Last-1;
          Step=0;
        }
// Otherwise increment/decrement the position
        else {
          FrameIndex+=Step;
        }
      }
    }
  }
  __finally {
    Synchronize(DeleteFrame);
  }
}
//--------------------------------------------------------------------------
-
void __fastcall TPlayThread::UpdateFrame(void)
{
// Display the frame in the main form
  Frame->Bitmap->Assign(JPG);
// Adjust the speed
  Pause=Velocity->Max-Velocity->Position;
// Just for test display the frame name
  Status->Caption=Current;
// Update the position on the ScrollBar to show the actual position in the
whole frames set
  ScrollBar->Position=FrameIndex;
}
//--------------------------------------------------------------------------
-
void __fastcall TPlayThread::DeleteFrame(void)
{
// Remove the allocated memory
  delete JPG;
  delete FrameList;
}
//--------------------------------------------------------------------------
-
Technical Details
Message-ID: <3ed1434d@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!38.144.126.75.MISMATCH!feed1.newsreader.com!newsreader.com!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail
References: <3ed00a13@newsgroups.borland.com> <3ed01446$1@newsgroups.borland.com> <3ed0adcc$1@newsgroups.borland.com> <3ed13011@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,9f6b69458a9a485d
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-05-25 15:32:08 PST
From: "Lapo" <lapoguidi@inwind.it>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3ed00a13@newsgroups.borland.com> <3ed01446$1@newsgroups.borland.com> <3ed0adcc$1@newsgroups.borland.com> <3ed13011@newsgroups.borland.com>
Subject: Re: TThread with messages
Date: Mon, 26 May 2003 00:28:54 +0200
Lines: 153
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
NNTP-Posting-Host: 62.98.129.123
Message-ID: <3ed1434d@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1053901645 62.98.129.123 (25 May 2003 15:27:25 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!38.144.126.75.MISMATCH!feed1.newsreader.com!newsreader.com!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail