View Full Version : TThread and VCL question
Magus
02-28-2008, 03:19 AM
I'm pretty new to threads. I have done some research and believe I am doing an 'OK' job. However, I have some 'for' loops that add nodes to a TreeView. When I call the function (which is in the Thread) it still locks up my entire application.
Here is what I'm working with:
K,L Array of Strings
UsersThread = Thread
if length(K) > 0 then
for j := 0 to length(K) - 1 do
begin
JJ := j;
UsersThread.RunLogons;
if Length(L) > 0 then
begin
tn := UsersThread.GetNodeByText(TreeView1,K[J],True);
for I := 0 to Length(L) - 1 do
with TreeView1.Items.AddChildFirst(tn, L[I]) do
begin
ImageIndex := IMG_NODE_USER;
SelectedIndex := IMG_NODE_USER;
end;
tn.Expanded := True;
Update;
end;
end;
I'm not sure what I can do to prevent my application form locking up. I'm already using those functions from within the thread and no luck. If anyone can help I would appreciate it.
Jasser
02-28-2008, 04:33 AM
When calling a function/procedure that update a VCL component inside a thread the function/procedure has to be called inside the Synchronize function. Example:
Synchronize(UpdateTreeView1);
Regards,
Abdulaziz Jasser
Magus
02-28-2008, 04:53 AM
I should have mentioned that the code I posted is not in the Thread. It's in my main form.
Jasser
02-28-2008, 05:12 AM
Ok..add the following line to the loop were you add the childs:
Application.ProcessMessages;
Regards,
Abdulaziz Jasser
Norrit
02-28-2008, 05:58 AM
Why you do this in a thread???
Anyway, I don't see how this will work inside a loop.
Inside the loop you call a function in the thread. In the meantime, the loop keeps processing, so it's undetermined if you add something and what you add.
I think the entire loop should be done inside the thread, and synchronize it back to the treeview...
Objective reality is a delirium caused by lack of alcohol in blood.
There is no place like 127.0.0.1
Magus
02-28-2008, 06:09 AM
I tried this but I couldn't get Synchronize to work. It compiles fine. However, once the thread begins to call the synchronized function is complains of an error and exits.
Magus
02-28-2008, 06:11 AM
Unfortunately this isn't working. My form is still locking up entirely.
Jasser
02-28-2008, 06:37 AM
Why you are using a thread? Threads are used to make things work in the background of the aplications (for long proccessing). In your case you should use a normal function with the code I posted before inside the loop. It should solve your problem. Try it without a thread and you will see.
Regards,
Abdulaziz Jasser
Magus
02-28-2008, 07:53 AM
It's still locking up. Not because of the TreeView it seems. GetLocalLogons() is causing the hangup. It's basically this line of code:
IF (RegConnectRegistry(pchar(ServerName), HKEY_USERS, usersKey) <> 0) THEN
Exit;
I tried putting most of the large functions in a separate thread thinking that would fix the problems. It didn't seem to solve anything. If I did everything correctly, that is.
Is there anything else I can use to keep this from hanging my application?
Jasser
02-28-2008, 09:57 AM
This is NOT a Thread-Safe function. Now you are learning more information about thread. NOT all functions are Thread_Safe. Let me put it this way:
This is NOT a Thread-Safe function. Now you are learning more information about thread. NOT all functions are Thread_Safe. Let me put it this way:
Some functions/procedures cannot be called from a thread. Normally we call them "NOT SUPPORT Thread-Safe mode". Therefore, the application will crash as soon you call them. My feeling is that you are using one of those functions which doesn't support Thread-Safe mode.
Regards,
Abdulaziz Jasser
Magus
02-28-2008, 10:11 AM
Yeah I'm learning. When I put everything in the thread and execute it, it gives me an exception error anytime trying to access a TreeView. I have a feeling that TreeView's aren't thread safe.
I also have a feeling that the RegistryConnect API isn't thread safe either.
Looks like I may be out of luck on both counts. Thanks for the help. =]
Norrit
02-28-2008, 11:43 AM
And what was the code and what was the error?
Objective reality is a delirium caused by lack of alcohol in blood.
There is no place like 127.0.0.1
Jasser
02-28-2008, 12:24 PM
No.. My friend, when you update someting on the screen (a VCL comonent) then you have to use "Synchronize" procudere. But if you are calling a function/procudere that do not update the screen and it fails, then it does not support Thread_Safe mode. Go back to the vendor and ask for that. Threads are not an easy subject.
Regards,
Abdulaziz Jasser
Magus
02-29-2008, 02:25 AM
I have been calling my VCL component from synchronize. I get an exception every time.
Magus
02-29-2008, 05:16 AM
Here is the code I'm using:
procedure TUsersThread.GetNodeByText;
var
Node: TTreeNode;
ATree: TTreeView;
AValue: String;
begin
AValue := K[JJ];
if frChild1.TreeView1.Items.Count = 0 then Exit;
Node := frChild1.TreeView1.Items[0];
while Node <> nil do
begin
if UpperCase(Node.Text) = UpperCase(AValue) then
begin
MyNode := Node;
MyNode.MakeVisible;
Break;
end;
Node := Node.GetNext;
end;
end;
I'm calling it by using Synchronize(GetNodeByText);
The gives an error when starting the if statement:
Access violation at address 0047CC4E in module 'MDItest.exe'. Read of address 00000364.
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.