Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi Threadin in delphi (Access violations)

Status
Not open for further replies.

vexy

Programmer
Oct 24, 2009
2
US
Hey guys, so I am writing an application that uses Multi threading API.

I keep getting access violation and I have no idea why even when I put a function in the thread that updates the list it gives me access violation after about 4-5 minutes.

What am I doing wrong?

Heres the code:

Top code:

Type
Void = Type Pointer;
var
Form1: TForm1;
t4id, t4h : Cardinal;

in start button:

t4h := CreateThread(nil, 1024, @t4,nil,0,t4id);

In body;

Procedure t4;SafeCall;
var
copyint:integer;
begin
sleep(505);
while True do
begin
sleep(1000);
form1.lstEmails.AddItem('hey4',form1.ssender);
// FetchHTML(getphone,3);
end;
end;
 
It's a good idea to use the Delphi TThread object to start up threads instead of the raw system API.

And you should know that the VCL is NOT THREAD SAFE.
Which means you can't willy-nilly call anything in the VCL, even a simple Add(), from a Thread. Well, you can, through the auspices of the Synchronize() method. Look it up.

Another way is to have your thread stuff its data into some global object (interlocking access first ), then have a separate TTimer object processing and posting that data to the VCL.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top