Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

ActiveForm with ThreadedTimer

Greetings,

Can someone tell me why this doesn't work? I've been trying to work this out and getting nowhere.

I'm having problems when trying to use threading on an ActiveForm (D7 on XP Pro). For a simple test, I created a new ActiveForm project and added a TMemo and ThreadedTimer (one I picked up on Torry's) to the form.

On the form's OnCreate I enable the timer. On the OnTimer event for the threaded timer component I add a line to the memo:

Memo1.Lines.Add('timer');

I use Web Deploy, then launch the resulting htm file in IE. The memo shows as expected but the memo never has any lines added.

If I replace the ThreadedTimer component with Delphi's standard TTimer and recompile, the ActiveForm works fine.

I have been trying to convert an existing app to ActiveForms for use in MMC. As I was doing this I found that anywhere I was using threads was not working.

Thanks - Eric

0
epaulson
Asked:
epaulson
  • 3
1 Solution
 
CSJCommented:
I am not familiar with the ThreadedTimer you found on Torry's.  However, when using objects from the VCL object hierarchy, their properties/methods are not necessarily thread-safe.  Try using the method Synchronize - this waits for the main VCL thread to enter the message loop and then executes the passed method:

procedure TMyThread.UpdateMemo;
begin
  mainForm.Memo1.Lines.Add('timer');
end;

procedure TMyThread.OnTimer(Sender: TObject);
begin
  Synchronize(UpdateMemo);
end;

Hope this helps

Floyd
0
 
epaulsonAuthor Commented:
Floyd,

Thanks for the suggestion! I will try this out and report back.

Eric
0
 
epaulsonAuthor Commented:
Sorry for the delay. Unfortunately I am covered as far as using the Sychronize method for the thread.

I have been unable to determine why doing this on an ActiveForm just doesn't work. If I do everything the same on a standard Delphi form it works fine. It seems that when using an ActiveForm that there must be something additional required to get this to work. I did experiment a bit to determine that the thread had a valid handle and that the timer was actually starting, but still no luck.

I am probably going to give up on doing it this way and look into using a separate DLL rather than threading.

Thanks

Eric
0
 
epaulsonAuthor Commented:
I figured it out! The key was to not use synchronize. I stumbled on this by experimenting with using the CreateThread API call and found I could get a thread to work in this manner. I took it a step further on my test program by creating a TThread. With the Synchronize(procedure) the thread would never execute properly, but worked fine without Synchronize. Since I am just using the thread to run a process without locking up my app, this will do just fine.

Eric
0
 
moduloCommented:
PAQ'd and 200 points refunded

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now