Solved

Best way to pass data from a custom thread to the main VCL thread WITHOUT Synchronize

Posted on 2006-11-23
10
488 Views
Last Modified: 2010-04-05
and continuation of this question (http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_22070798.html)

title says it all: we have
- indy
- reading thread on client side
- preferably no synchronize (though I am willing to accept that if it cannot be done otherwise)
- elegant AND simple solutions please (though you can post some interesting "hacks" if you know any (like the one with the timer I mentioned in the other question - keep in mind that it doesn't use a thread so it's no good for me, but it might be good for future references))

PS: in case I missed some obvious solution, my brain is seriously tired :)
0
Comment
Question by:2266180
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 19

Accepted Solution

by:
MerijnB earned 100 total points
ID: 18003684
don't have much time to type a long story atm. But what I use often is a system which allocate enough room in a pchar, send the address of the pchar to the receiver (could very well be the main thread). Use PostMessage to make this async. The receiver has to deallocate the memory. If you want to send more then a string you can use a pointer to a record or whatever you can think of :)
0
 
LVL 28

Author Comment

by:2266180
ID: 18003749
windows messages. how could I have missed that? (And I thought about pipes and sinchronized classes and other more complicated things. my brain is tired: misses all the easy stuff.)
so it's a good idea. it's even faster to implement starting with the code I have. thanks.
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 18003797
i'll don't bother for an example, yw :)
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 28

Author Comment

by:2266180
ID: 18003817
no need to :)
thought I will wait for other ideas as well :)
0
 
LVL 21

Assisted Solution

by:ziolko
ziolko earned 100 total points
ID: 18003914
yup, PostMessage() as I posted in previous Q:)

other idea is to create TObjectList descendant with Push() and Pop()
methods so reader will Push() read data and then PostMessage() to reciver
that something is in buffer, reciver will Pop() in this case TCriticalSection will be
needed for push & pop  

ziolko.
0
 
LVL 28

Author Comment

by:2266180
ID: 18003939
I thought about that. issue is that we are doing a communication here (see the demo app in the other Q so I have to consume the data the thread read (by using pop) and the data that waits for me on the connection. using a stack and postmessage will mess up things :)
BUT if you read all need data from the thread, then indeed using a stack and postmessage is an elegant solution.
for this specific case though, just a sendmessage is better and simpler. (reading rest of the data in the message handler)
0
 
LVL 13

Assisted Solution

by:BlackTigerX
BlackTigerX earned 100 total points
ID: 18004077
wouldn't be easier using a memory mapped and simple post messages?
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 18004638
if it hasn't been mentioned yet, you could use a TCriticalSection (although I'm not a fan of those in threads personally)
and wm_copydata is another option.
there is also the slow memory method using a mutex for locking, or in fact
the very slow "writing data to a file", and using standard file locking, to know if the main vcl can read from it.
0
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 100 total points
ID: 18004735
"shred memory" uising a mutex I meant
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 100 total points
ID: 18006312
Wished I had something to add but so far the answer seems to have been given already. :-)
I had to deal with a similar situation in the past and solved it in about the same way. One thread would allocate memory on the stack and then Send/Post a message to another thread, allowing the other thread to start processing the data. Worked like a charm, most of the time. Biggest problem is deciding about using either PostMessage or SendMessage. SendMessage will pause the current thread until the message returns with a reply. PostMessage is therefore a bit nicer but its drawback is that when the thread ends that did the PostMessage and the message has not been handled yet, the message will disappear in an eternal limbo.

So for a recent project of mine, I chose a slightly different solution by using a (file-based) queue. Instead of posting messages, you would have one thread pushing items to the queue and the other thread popping the data from the queue. If your queue is just a linked list of pointers then you would only need to use a critical section in the push/pop functions to protect the queue. (But not the data!) Your queue item would be something like this:

type
  PQueueItem = ^TQueueItem;
  TQueueItem=record
    Data: Pointer; // Points to your data structure.
    Next: PQueueItem;
  end;

Creating this item (with New) can be done outside the critical section. It's just the adding and deleting of queue items that needs to be protected. And while I use a file-based queue since my data happens to be quite big, you could just keep this queue in-memory, of course.

Still, a solution like this queue has also been suggested before in this thread. :-)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

776 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