Reputation: 127
I'm having trouble running my code in a tthread object. Its supposed to go through all the links in twebbrowser and then copy it to a memo if the url contains 'http://www.ebay.com/itm/'. The code works perfectly in in the mainform but doesn't work in a tthread object.
My Code:
begin
count := 0;
loop := 0;
repeat
link := Webbrowser1.OleObject.Document.Links.Item(loop).href;
if AnsiContainsStr(link, 'http://www.ebay.com/itm/') then
begin
Form1.Memo1.Lines.Add(link);
end;
end;
loop := loop + 1;
count := count + 1;
until count = Webbrowser1.OleObject.Document.links.Length;
end;
The error I'm getting
Access violation at address xxxxxx in module mshtml.dll
Thanks for reading and all replies.
Upvotes: 4
Views: 1454
Reputation: 612993
Your control still has afinity to the GUI thread. So you cannot access it from the worker thread.
You just need a slight adjustment to the threading design to make this work. Process the page's source to pull off all the links in the GUI thread. This has to be done there, you have no choice. But then let the worker thread do the onward processing of the links. I'm assuming the real app does more with the links than put them in a memo!
You should have a simple producer/consumer design. The GUI thread produces a list of links. The worker thread(s) consume them. A threadsafe queue will allow you to synchronize tasks between the threads. And you may benefit from using a higher level library like OTL in preference to coding raw threads.
Upvotes: 1