Article View: borland.public.cppbuilder.nativeapi
Article #4697Re: Synchronized call
From: tinyabs
Date: Mon, 05 Dec 2005 08:06
Date: Mon, 05 Dec 2005 08:06
77 lines
3272 bytes
3272 bytes
Here's a pseudo coding.
typedef void (__closure *TMyMethod)(LPVOID lpParameter);
typedef struct
{
TMyMethod Method;
LPVOID Parameter;
} TSyncMarshal;
class TSynchronize
{
public:
TSynchronize()
{
FMessageId = RegisterWindowMessage("MyEventMoniker");
FAppEvents.Reset(new TApplicationEvents(NULL));
FAppEnvets->OnMessage = this->OnMessage;
}
void Synchronize(TMyMethod method, LPVOID lpParameter)
{
// psuedo code
if MainThread == CurrentThread then
method(lpParameter)
else
{
auto_ptr<TMyWin32Event> notifyEvent(new
TMyWin32Event);
TSyncMarshal syncMarshal;
syncMarshal.Method = method;
syncMarshal.Parameter = lpParameter;
PostMessage(Application->MainForm->Handle, FMessageId,
(UINT) &syncMarshal, (UINT) notifyEvent.get()); // only PostMessage works
across threads
notifyEvent.WaitOne(INFINITE);
}
}
private:
void __fastcall OnMessage(tagMSG &Msg, bool &Handled)
{
if (Msg.message == FMessageId)
{
TSyncMarshal *syncMarshal = (TSyncMarshal *)
message.wParam;
TMyWin32Event *event = (TMyWin32Event *) message.lParam;
syncMarshal->Method(syncMarshal->Parameter);
notifyEvent->Set();
Handled = true;
}
}
UINT FMessageId;
auto_ptr<TApplicationEvents> FApoEvents;
};
TSynchronize gSynchronizeObj = NULL;
void Synchronize(TMyMethod method, LPVOID lpParameter)
{
if (gSynchronizeObj == NULL)
gSynchronizeObj = new TSynchronize();
gSynchronizeObj(method, lpParameter);
}
You should note that Synchronize() type of function should be used to update
UI only. Involving your objects that have events in a Synchronize() call
might cause deadlocks.
"Bogdan" <boprea@xnet.ro> wrote in message
news:438e1a53@newsgroups.borland.com...
> Hello,
>
> How can I synchronize a function call from a thread using API functions.
> I need to do what Synchronize(Function) from TThread does, but
> with Win 32 API.
>
> Thanks
> Bogdan
>
Technical Details
Message-ID:
<4393f504$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<438e1a53@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,38c7fb08131702ad X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "tinyabs" <nospam@me.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <438e1a53@newsgroups.borland.com> Subject: Re: Synchronized call Date: Mon, 5 Dec 2005 16:06:08 +0800 Lines: 79 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 NNTP-Posting-Host: 222.165.0.181 Message-ID: <4393f504$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1133769988 222.165.0.181 (5 Dec 2005 00:06:28 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail