Solved

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

Posted on 2006-11-23
10
489 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

831 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