Solved

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

Posted on 2006-11-23
10
483 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:ciuly
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 19

Accepted Solution

by:
MerijnB earned 100 total points
Comment Utility
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:ciuly
Comment Utility
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
Comment Utility
i'll don't bother for an example, yw :)
0
 
LVL 28

Author Comment

by:ciuly
Comment Utility
no need to :)
thought I will wait for other ideas as well :)
0
 
LVL 21

Assisted Solution

by:ziolko
ziolko earned 100 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

Author Comment

by:ciuly
Comment Utility
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
Comment Utility
wouldn't be easier using a memory mapped and simple post messages?
0
 
LVL 17

Expert Comment

by:TheRealLoki
Comment Utility
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
Comment Utility
"shred memory" uising a mutex I meant
0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 100 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now