 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Muhammad Momin Rashid Guest
|
Posted: Sun Nov 23, 2003 9:23 am Post subject: Help required with Synchronize function |
|
|
Hello,
I have a recursive function which calculates the size of a given directory
(and directories below that level). I need to update the TreeView Component
with the directory struction along with the information returned by this
function. Everything was working smoothly before i tried switching to using
threads.
The function that updates the TreeView Component needs two parameters
(Current Node and Value for Child Node), and it should return a pointer to
new Child node created. I am not sure how to make a call this function
using Synchronize, I tried Synchronize (UpdateDir) but it does not compile
as UpdateDir need parameters and returns a value. Can anyone guide me on
how to accomplish this?
Regards,
Muhammad Momin Rashid.
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sun Nov 23, 2003 10:43 pm Post subject: Re: Help required with Synchronize function |
|
|
"Muhammad Momin Rashid" <momin (AT) hotmail (DOT) com> wrote
| Quote: | The function that updates the TreeView Component needs two
parameters (Current Node and Value for Child Node), and it
should return a pointer to new Child node created. I am not sure
how to make a call this function using Synchronize, I tried
Synchronize (UpdateDir) but it does not compile as UpdateDir
need parameters and returns a value. Can anyone guide me on
how to accomplish this?
|
You need to store your parameters into separate members of the thread class
first, and then have Synchronize() call a method that takes no parameters,
which then calls the real function passing it the variables. For example:
class TMyThread : public TThread
{
private:
TTreeNode *CurrentNode;
AnsiString ChildValue;
TTreeNode *NewNode;
//...
void __fastcall DoUpdateDir();
//...
};
void __fastcall TMyThread::Execute()
{
//...
CurrentNode = whatever;
ChildValue = whatever;
Synchronize(DoUpdateDir);
// use NewNode as needed
//...
}
void __fastcall TMyThread::DoUpdateDir()
{
NewNode = UpdateDir(CurrentNode, ChildValue);
}
Gambit
|
|
| Back to top |
|
 |
Muhammad Momin Rashid Guest
|
Posted: Mon Nov 24, 2003 12:45 am Post subject: Re: Help required with Synchronize function |
|
|
Hi,
Thanks for the responsse.
I had already tried this solution, but the problem is a bit complicated
given that the fucntion (that is calculating the size of directory = file
sizes + size of sub directories) is a recursive one. If a sub directory is
found that a recursive call is generated, and the values of CurrentNode and
CurrentValue gets changed.
The following code should elaborate my point. Probably I am overlooking
something very simple here.
void __fastcall TMyThread::Execute()
{
RecursiveFunction(StartNode, StartNodeValue)
}
void __fastcall TMyThread::RecursiveFunctin(PassedCurrentNode,
PassedCurrentNodeValue)
{
//...
CurrentNode = whatever;
ChildValue = whatever;
for each item in the CurrentDirectory
If ( Sub Directory Found )
{
Synchronize(DoAddDir);
dirSize += RecursiveFunction(subdir, subdirvalue)
}
else (if file found)
{
// ...
dirSize += file size
}
// if recursive call happens NewNode gets changed
//...
Synchronize (DoUpdateDir)
}
Regards,
Muhammad Momin Rashid.
"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote
| Quote: |
"Muhammad Momin Rashid" <momin (AT) hotmail (DOT) com> wrote in message
news:3fc07c9a (AT) newsgroups (DOT) borland.com...
The function that updates the TreeView Component needs two
parameters (Current Node and Value for Child Node), and it
should return a pointer to new Child node created. I am not sure
how to make a call this function using Synchronize, I tried
Synchronize (UpdateDir) but it does not compile as UpdateDir
need parameters and returns a value. Can anyone guide me on
how to accomplish this?
You need to store your parameters into separate members of the thread
class
first, and then have Synchronize() call a method that takes no parameters,
which then calls the real function passing it the variables. For example:
class TMyThread : public TThread
{
private:
TTreeNode *CurrentNode;
AnsiString ChildValue;
TTreeNode *NewNode;
//...
void __fastcall DoUpdateDir();
//...
};
void __fastcall TMyThread::Execute()
{
//...
CurrentNode = whatever;
ChildValue = whatever;
Synchronize(DoUpdateDir);
// use NewNode as needed
//...
}
void __fastcall TMyThread::DoUpdateDir()
{
NewNode = UpdateDir(CurrentNode, ChildValue);
}
Gambit
|
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Nov 24, 2003 1:29 am Post subject: Re: Help required with Synchronize function |
|
|
"Muhammad Momin Rashid" <momin (AT) hotmail (DOT) com> wrote
| Quote: | I had already tried this solution, but the problem is a bit
complicated given that the fucntion (that is calculating the
size of directory = file sizes + size of sub directories) is a
recursive one.
|
If itis such a problem to accomplish, then you'll just have to populate the
TreeView separately. Run the recursive function first and just gather all
the necessary data (such as into a double-linked list so that you can
preserve the tree hierarchy of the data), and then popuplate the TreeView
afterwards with the gathered data, not while the traversal is occuring. The
alternative is to populate the TreeView beforehand with the folder
structure, and then have the thread just update the existing nodes with the
data without needing to add anything new to it.
Gambit
|
|
| Back to top |
|
 |
Muhammad Momin Rashid Guest
|
Posted: Tue Nov 25, 2003 8:22 am Post subject: Re: Help required with Synchronize function |
|
|
I was afraid it would come to something like that, but I was hoping for a
more elegant way of accomplishing this task.
Regards,
Muhammad Momin Rashid.
"Remy Lebeau (TeamB)" <gambit47.no.spam (AT) no (DOT) spam.yahoo.com> wrote
| Quote: |
"Muhammad Momin Rashid" <momin (AT) hotmail (DOT) com> wrote in message
news:3fc154c4 (AT) newsgroups (DOT) borland.com...
I had already tried this solution, but the problem is a bit
complicated given that the fucntion (that is calculating the
size of directory = file sizes + size of sub directories) is a
recursive one.
If itis such a problem to accomplish, then you'll just have to populate
the
TreeView separately. Run the recursive function first and just gather all
the necessary data (such as into a double-linked list so that you can
preserve the tree hierarchy of the data), and then popuplate the TreeView
afterwards with the gathered data, not while the traversal is occuring.
The
alternative is to populate the TreeView beforehand with the folder
structure, and then have the thread just update the existing nodes with
the
data without needing to add anything new to it.
Gambit
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|