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: 335
  • Last Modified:

Thread question...

I'm currently recoding an old app, because it was too ugly for me..=)

Now i am unsure if where events would fire....

If i create a tthread descendant like this...

Type
  TMyThread = Class(TThread)
  Private
    fMyComp : TMyComponent;
  Protected
    Procedure Execute;
  Public
    Procedure SomeSillyEvent(Sender: TObject);
    Constructor Create;
    Destructor Destroy;
  End;


Constructor Create
Begin
  Inherited Create(True);
  CreateMyComponent; //P-Code, it just does it.
  fMyComp.MyTriggerEvent := SomeSillyEvent;
  Suspended := FALSE;
End;

Procedure Execute;
Begin
  While Not Terminated do
    Sleep(5);
End;

Where would the trigger event run ?
Inside the thread or inside the thread that called the TMyThread.Create; ?

Hoping for a speedy response.. =)

Regards
//raidos
0
raidos
Asked:
raidos
  • 3
  • 2
  • 2
  • +2
1 Solution
 
swift99Commented:
the event would run inside the original thread that created the object, the way it is coded now.


0
 
raidosAuthor Commented:
That's what i figured...

If i move the CreateMyComponent into the Execute then..
will it fire inside the TMyThread then ?
0
 
Martyn SpencerConsultantCommented:
Hmm, not so sure about swift's answer... Correct me if I am wrong, but I would say that the event code will be called in the main vcl thread, since I presume that the component is a vcl component, all which use the main thread. What are you looking to acheive? I may be able to offer a suggestion, or two...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
raidosAuthor Commented:
The component is a TApdComport, Turbo Power Async Pro, it is a  nonvisual component.

Hope i can get some more input on this..

Regards
//raidos
0
 
Martyn SpencerConsultantCommented:
Ok. Am I to take it that you are looking to process the received data in a thread? If so, then you could look into the possibility that the event handler actually stores the data to be processed by the thread and then signals it to do its work. Clearly, this depends on what you are looking to do. May I ask why you are looking to handle the event in a thread, rather than processing in the main thread? Clearly serial comms is quite a slow process, and the OS itself provides a degree of abstraction away from this and I would imagine that on a reasonable spec PC, the need for threaded processing of serially gathered data is not too high. If you want to tell me more about what you are looking to do, I may be able to help further - got a few years of Asynch pro experience under my belt :o)
0
 
swift99Commented:
the owning thread for any component is the thread that creates it.  I've just been through that the hard way  :o)

If you create an object under a thread then it will respond to events in that thread's queue, unless you explicitly transfer ownershp of the object to another thread.

I don't know his reasons for doing this, but I tend to run commmunications in its own thread, except for a telnet service (also in its own thread) that allows me to debug and tweak apps settings remotely (and get state info if it locks up).  This decouples the GUI from the services layer, buying better responsiveness from both the comm and the GUI.

However, my comm thread events merely acknowldege receipt, package up the received bytes, and stash them in a queue for the "worker" threads to deal with.  I don't do any application processing in the comm thread.

To achieve your apparent goal, non-blocking asynchronous I/O, the code should be something like this.  Note that I didn't check this code or the exact form of the API calls.  I leave that as an exercise for you.


Type
 TMyThread = Class(TThread)
 Private
   fMyComp : TMyComponent;
 Protected
   Procedure Execute;
 Public
   Procedure SomeSillyEvent(Sender: TObject);
   Constructor Create;
   Destructor Destroy;
 End;


Constructor TMyThread.Create
Begin
 // this code is executed within the parent thread
 // so any component create here belongs to the parent thread
 Inherited Create(True);
 Suspended := FALSE;
End;

Procedure TMyThread.Execute;
var
  msg: TMsg;
Begin
 // this code is executed under MyThread
 // so any component create here belongs to this instance of MyThread
 CreateMyComponent; //P-Code, it just does it.
 try
   fMyComp.MyTriggerEvent := SomeSillyEvent;
   While Not Terminated do
   begin
     res := GetNextMessage (msg, 0, 0, 0); // wait for any event
     TranslateMessage (msg);
     DispatchMessage (msg);

     case msg.message of
     Xxx: HandleXxx (Msg);
     end;
   end;
 finally
   DestroyMyComponent; //P-Code, it just does it.
 end;
End;

0
 
raidosAuthor Commented:
Thanks swift...looks similar to what I would have guessed, i'll post a new question for you with points.

mdspencer, the reason i want to move these things into a thread is simple...they don't need much processing power so there is no reason to have it hanging in the main thread should something go terribly wrong.

Also the application may contain 4 concurrent comport each doing something different than the other...

The application is a NT service, so i do not like having anything that even if not crash-prone, must never stop the main thread, if some error occurs within the thread...i can just free it and restart it.

Anyway...it is somewhat important...
0
 
CleanupPingCommented:
raidos:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
snehanshuCommented:
Hi!
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: swift99

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

...Snehanshu
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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