?
Solved

Thread question...

Posted on 2003-03-10
9
Medium Priority
?
332 Views
Last Modified: 2011-09-20
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
Comment
Question by:raidos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Expert Comment

by:swift99
ID: 8106565
the event would run inside the original thread that created the object, the way it is coded now.


0
 
LVL 3

Author Comment

by:raidos
ID: 8106738
That's what i figured...

If i move the CreateMyComponent into the Execute then..
will it fire inside the TMyThread then ?
0
 
LVL 2

Expert Comment

by:mdspencer
ID: 8106750
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
Technology Partners: 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!

 
LVL 3

Author Comment

by:raidos
ID: 8106871
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
 
LVL 2

Expert Comment

by:mdspencer
ID: 8107490
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
 
LVL 6

Accepted Solution

by:
swift99 earned 400 total points
ID: 8108126
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
 
LVL 3

Author Comment

by:raidos
ID: 8109413
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
 

Expert Comment

by:CleanupPing
ID: 9316853
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 10090747
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question