More efficient Synchronizing method?

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

More efficient Synchronizing method?

Postby Sailor » Tue Feb 01, 2005 2:11 pm

Hi,

My application consist of a secondary thread that constantly updates the VCL with new info in e.g Labels, ProgressBars ListViews etc. I tend to like CBuilder's thread safe metod Synchronize. Unfortunately it demands a direct jump to another metod so my application seems to be drawning in short synchronizing methods as e.g. below:

void __fastcall TMyThread::Execute()
{
Synchronize(UpdateLabel1);
}


void __fastcall TMyThread::UpdateLabel1(void)
{
Form1->Label1->Caption = "Hello";
}

It would have been much more efficient if one could use something like:
Synchronize(Form1->Label1->Caption = "Hello") directly, but unfortunately this apparently does not work...

Any tip on something similar to save code?
Sailor
 

Postby Damon » Wed Feb 02, 2005 6:33 pm

Hi Sailor,
There are a couple of other synchronization techniques mentioned here...

http://bcbjournal.org/get_doc.php?art=subscribers/articles/vol8/0104/Jan04-01.htm

but none of them are easier-to-use than the Synchronize() method. Another approach is to define a utility method, like so...

Code: Select all
class TMyThread : public TThread
{
public:
  __fastcall TMyThread(bool CreateSuspended);

protected:
  void __fastcall Execute();

private:
  void __fastcall UpdateControl()
    {
      // set the Caption (Text) of the control
      CurrentControl_->SetTextBuf(CurrentCaption_.c_str());
    }
  void SyncUpdateControl(TControl* Control, AnsiString Caption)
    {
      CurrentControl_ = Control;
      CurrentCaption_ = Caption;
      if (CurrentControl_ != NULL)
      {
        Synchronize(UpdateControl);
      }
    }

private:
  TControl* CurrentControl_;
  AnsiString CurrentCaption_;
};


Then, you'd just call...

Code: Select all
SyncUpdateControl(Form1->Label1, "Hello");


You may also be able to accomplish what you want by using macros, but in the end, all of these approaches might turn out to be more work than the approach you're already doing.

Good luck,
Damon
User avatar
Damon
BCBJ Editor and Admin
BCBJ Editor and Admin
 
Posts: 285
Joined: Wed May 26, 2004 11:25 pm
Location: Stillwater, OK, USA

Postby Sailor » Fri Feb 04, 2005 7:50 am

Hi Damon.

Many thanks for the utility method, I think this was exactly what I had in mind!

I spent a couple of hours today but failed in my attempts trying to update corresponding methods for:

SyncUpdateControl2 (Form1->ProgressBar1, 25);
SyncUpdateControl3 (Form1->ListBox1->Items->Add(), "Hello");

Obviously, to update the following line is crucial:

CurrentControl_->SetTextBuf (CurrentCaption_.c_str());

Any ideas on how I should make correct updates?

Many thanks (again) in advance

PS. Yor articles covering Multi threading I-III are great and have helped considerable on other thread issues :D
Sailor
 

Postby Sailor » Fri Feb 04, 2005 1:36 pm

Hi again,

No worries, I finally managed it myself.

I listed it below in case someone else would benefit from it.

best regards
/Sailor

class TMyThread : public TThread
{
public:
__fastcall TMyThread(bool CreateSuspended);

protected:
void __fastcall Execute();

private:
TProgressBar* currentProg_;
int currentPos; // For ProgressBars
TListBox* currentLb_;
AnsiString currentMsg; // For ListBoxes

Private:

// ListBox
// --------------------------------------------------------------------
void __fastcall UpdateLbox()
{
// Synchcronizing ListBoxes
currentLb_->Items->Add(currentMsg);
}

void SyncListBox(TListBox* lb, AnsiString message)
{
// Synchcronizing ListBoxes
currentLb_ = lb;
currentMsg = message;
if(currentLb_ != NULL)
Synchronize(UpdateLbox);
}
// --------------------------------------------------------------------


// ProgressBars
// --------------------------------------------------------------------
void __fastcall UpdatePbar()
{
// Synchcronizing ProgressBars
currentProg_->Position = currentPos;
}

void SyncProgressBar(TProgressBar* pb, int pos)
{
// Synchcronizing ProgressBars
currentProg_ = pb;
currentPos = pos;
if(currentProg_ != NULL)
Synchronize(UpdatePbar);
}
// --------------------------------------------------------------------

}
Sailor
 


Return to Technical

Who is online

Users browsing this forum: No registered users and 17 guests

cron