🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2632

Callback, Dll, Thread and synchronization HELP!

#2632
From: Stefano Bonifazi
Date: Wed, 07 Feb 2007 19:42
91 lines
3307 bytes
Hi All:)
  I have a complicate (for my skills) problem..
I have a console apllication. It loads a dll that I wrote.
This dll works like a TCP Client. It creates a thread from within it 
receives data from a TCP connection.
The console application passes a callback function to the initialization 
function of the dll. Inside the dll's thread the callback is called to 
process the data received. All works fine if I call the callback 
function from within the thread.
But when I try to call it from a method called with Synchronize inside 
the thread, it stops working :( The not working code:

Inside the dll:

typedef void (*RECEIVETXTPROC)(char* Txt);
RECEIVETXTPROC OnReceiveTxt;

bool Initialize(char* ServerIPAddress,
                 int Port,
                 bool ActivateAsynchronousReception,
                 RECEIVETXTPROC OnReceiveCallback,
                 bool UseStartingDelimiter,
                 char StartingDelimiter,
                 char EndingDelimiter)
{
  ...
   OnReceiveTxt=OnReceiveCallback;
  ...
}

class TReceiverThread : public TThread
{
   public:
    AnsiString RvdTxt;
  ...
}


//the following class is responsable of creating and destroying the thread.

class AsynchronousReceiverClass
{
  private:
   TReceiverThread* ReceiverThread;
  ...
  public:
   void __fastcall ExecuteCallBack();
  ...
}

void __fastcall AsynchronousReceiverClass::ExecuteCallBack()
{

     if(OnReceiveTxt)
      OnReceiveTxt(ReceiverThread->RvdTxt.c_str() );

}
AsynchronousReceiverClass* AsynchRec=NULL;

void __fastcall TReceiverThread::Execute()
{
  ...
  RvdTxt=Txt;//Txt contains the received string
  //AynchRec is a global instance of AsynchronousReceiverClass
  Synchronize(AsynchRec->ExecuteCallBack);
  ...
}

//Inside the console application:

void OnAsynchReceiveCommand(char* Buffer)
{
// do stuff with the string
}

//the following is loaded from the dll
Initialize(ServerIPAddress,
                     Port,
                     false,
                     OnAsynchReceiveTxt,//callback for asynch reception
                     true,
                     '�',
                     '�'
                    );


if I just put the code written inside ExecuteCallBack() into the Thread 
all works fine, even if I think it's not thread safe :(
What should I do?
Thank U in advance,
Stefano B.
Technical Details
Message-ID: <45ca2bba@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,7631859e1e5bf75d,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII
Date: Wed, 07 Feb 2007 20:42:54 +0100
From: Stefano Bonifazi <stefboombastic@email.it>
User-Agent: Thunderbird 1.5.0.9 (Windows/20061207)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Callback, Dll, Thread and synchronization HELP!
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
NNTP-Posting-Host: 80.104.235.73
Message-ID: <45ca2bba@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1170877370 80.104.235.73 (7 Feb 2007 11:42:50 -0800)
Lines: 91
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail