Delphi - Thread not executing in ActiveX form - but does elsewhere
Asked Answered
E

2

5

I have a thread, called TAlertThread. The thread interacts with its owner by triggering events. For example, when certain data is available inside the thread, it sets some temp variables and calls Synchronize(UpdateAlert) which in turn triggers the appropriate event.

Now the thread works perfectly in any standard windows application. My problem is when I put that thread inside of an ActiveX form (TActiveForm). The ActiveX control (aka COM object) is then embedded inside of a Windows Desktop Gadget (via HTML / Javascript). I also have experience with this, the gadget is not the issue. The ActiveX component works fine in its destination, except the thread is never executed. It's even being called EXACTLY the same way as I called it from the App.

Is this some limitation with ActiveX, blocking threads from executing? I wouldn't think so, because other things that require threads internally (such as TADOConnection) work. I am in fact properly calling CoInitialize and CoUninitialize appropriately. Again, works perfect in an application, but does not work at all in ActiveX.

Here is how I call this thread...

procedure TRMPDashXS.ExecThread;
begin
  //Thread created suspended
  lblStatus.Caption:= 'Executing Thread...'; 
  fThread:= TAlertThread.Create(fConnStr); //fConnStr = connection string
  fThread.Priority:=      tpIdle;
  fThread.OnConnect:=     Self.ThreadConnected;
  fThread.OnDisconnect:=  Self.ThreadDisconnected;
  fThread.OnBegin:=       Self.ThreadStarted;
  fThread.OnFinish:=      Self.ThreadFinished;
  fThread.OnAlert:=       Self.ThreadAlert;
  fThread.OnAmount:=      Self.ThreadAmount;
  fThread.Resume; //Execute the thread
end;
Ecg answered 28/10, 2011 at 23:26 Comment(10)
To clarify, I mean "Not working" as in the Execute procedure is never called by the thread.Ecg
Umm.. missing 'override' on execute method declaration? Just a guess - I've never seen anything like this happen. My threads always reach the first line of the execute method before blowing up.Adamantine
Oh - 'tpIdle'. Try raising that to tpNormal, just for now..Adamantine
Yes it does have override on the execute procedure, and I tried the priority, no help. Thread works when used in any windows app, but once put inside ActiveX, it won't work anymore. So I'm sure it has to do with ActiveX, but I need some confirmation so I know that's right.Ecg
And you don't get an error? The label caption changes? If so, can you change it again, after calling Resume? Does that work?Woodrum
Synchronize requires co-operation from the host app. I can't see that a Windows desktop gadget is going to help you there. Try sending a message instead.Disembogue
Oh, and yes, threading in COM/ActiveX is a little funky.Disembogue
I do not get any error, I set the label caption after resume and it worked, so it is in fact successfully calling Resume. I posted the same question here with all my source code: tek-tips.com/viewthread.cfm?qid=1665126Ecg
And what do you mean by "Synchronize requires co-operation from the host app"? It's calling a procedure which is part of the same thread object, which in turn triggers an event. I tried putting the ActiveX control in a C# project and same issue.Ecg
Look at the implementation of Synchronize. It places the method to be executed on the main thread into a list and then waits until it has been processed. Meanwhile, the main thread needs to know of the existence of this list, check it, process any methods, and signal their completion. This is what I mean by cooperation. You need you main thread to partake in this process and I bet it is not doing so. I don't know enough about ActiveX to be confident about this or even to suggest a solution.Disembogue
B
7

I suspect this might describe exactly what you're experiencing in your version of Delphi:

I'm not sure if that helps ... but I hope it does. At least a little :)

PS: Is there any particular reason you have to use Com/ActiveX and/or TActiveForm?

Brackett answered 29/10, 2011 at 21:14 Comment(6)
+1 These articles explain the issues I was alluding to in the comments.Disembogue
My particular reason was because A) Delphi is my primary language, B) product must be in a Gadget, C) Delphi supports easily creating ActiveX forms, and D) I COULD use AJAX in this Gadget, but do not want to implement an HTTP server in order to do so. This ActiveX control connects directly to a SQL database. I may wind up abandoning the idea of making a Gadget and instead build a tray application (hidden until needed). The objective is to provide alerts/notifications to user for our software, when the software isn't running. Software uses SQL database, and javascript cannot connect to SQL.Ecg
Maybe your ActiveX control won't be able to communicate with the SQL database, either. If your product is remote, and if there's one or more firewalls in between your customer and your company's SQL Server that you don't have control over, then you're SOL. Q: Do you really need TActiveForm (re. your original question). Q: Do you really need SQL server? And if so, can it be a local (embedded) SQL Server, that's literally installed on your customer's desktop (and doesn't need to communicate with anything else)?Brackett
HALLELUIAH! It worked! I was so sure it wasn't going to work, but it did! Thank you, can't thank you enough! Hopefully I will get that bonus now I've been waiting for!Ecg
btw, the software is 16 years old, SQL is required on the server, all client PC's do not have SQL, but connect to it. Waking the main thread worked, that's the important part, thank you againEcg
To be more specific, it's a very large inventory/point-of-sale software package which I'm working around, cannot change how the system works (like SQL etc.) but since that worked, I'm ok nowEcg
E
0

According to this article here: http://edn.embarcadero.com/article/32756 web browsers don't allow threading via ActiveX. However that still doesn't explain why it doesn't work when I put it in a C# application.

Ecg answered 29/10, 2011 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.