DDE conversation in main thread AND worker thread


2006-08-23 03:22:38 AM
cppbuilder11
I've been searching the groups for days trying to figure out a way to
create two DDE conversations. One in the main thread, and one in the
worker thread. I can make it work currently in one or the other, but
not both at the same time. From what i've read, this has to do with
the ddeMgr. Is there a way to create a second instance of this...i
guess a better question is, how do I make my thread use this second
instance? I keep seeing things like "make sure to call DDEInitialize
from the same thread", but I don't even make that call. I use
TDDEClientConv which I assume makes the call from within it's function.
I've read the stuff about needing message loops and callback, etc. I
don't really understand the callback part, but i have put in the
message loops to no avail. Here is the code i use to create the
object:
ActiveDDECountThr = 5; //
strcpy(DDEServiceName,"rslinx");
strcpy(DDETopicNameSorterCThr,"THREAD");
// Create the array of DDE Client conversations
ThreadForm = new TForm(Application);
DDEArrayThr = (TDdeClientConv **)
malloc(sizeof(TDdeClientConv*)*ActiveDDECountThr);
DDEConnectionArrayThr = (DDEConnectionRingRec **)
malloc(sizeof(DDEConnectionRingRec*)*ActiveDDECountThr);
for (int i = 0; i < ActiveDDECountThr; i++) {
DDEArrayThr[i] = new TDdeClientConv(ThreadForm);
DDEArrayThr[i]->ConnectMode = ddeManual;
DDEArrayThr[i]->CloseLink();
DDEConnectionArrayThr[i] = new DDEConnectionRingRec();
DDEConnectionArrayThr[i]->Connected = false;
}
Here's the code used to connect:
void __fastcall TDivConfThread::OpenDDELinkThr(int i)
{
char ServiceName[40];
char TopicName[40];
char Description[40];
char Msg[255];
strcpy(ServiceName,DDEServiceName);
strcpy(TopicName,DDETopicNameSorterCThr);
strcpy(Description,"Test Thread DDE Connection");
DDEArrayThr[i]->CloseLink();
DDEConnectionArrayThr[i]->Connected = false;
strcpy(DDEConnectionArrayThr[i]->ServiceName,ServiceName);
strcpy(DDEConnectionArrayThr[i]->TopicName,TopicName);
strcpy(DDEConnectionArrayThr[i]->Description,Description);
DDEArrayThr[i]->SetLink(ServiceName,TopicName);
bool DoIt = false;
if (DDEArrayThr[i]->OpenLink()) {
DDEConnectionArrayThr[i]->Connected = true;
sprintf(Msg,"DDE #%d Linked to
%s",i,DDEConnectionArrayThr[i]->Description);
} else {
sprintf(Msg,"DDE #%d NOT LINKED to %s. ServiceName: %s, TopicName:
%s
",i,DDEConnectionArrayThr[i]->Description,DDEServiceName,DDETopicNameSorterCThr);
}
strcpy(SyncMsg,Msg);
Synchronize(SendThreadDataToMainFormMemo);
}
The OpenLink() always fails, apparently because ddeMan was created in
the mainthread. If I do this:
if (ddeMgr != NULL) delete ddeMgr;
ddeMgr = new TDdeMgr(Application);
OpenLink() will work in the worker thread, but not the main thread.
Any thoughts on how to get it to work in both? Thank you very much in
advance,
Ben