Solved

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

Posted on 2006-11-23
10
490 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
[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
  • +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
Independent Software Vendors: 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 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

737 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