TnFox application in DLL and threads

View: New views
3 Messages — Rating Filter:   Alert me  

TnFox application in DLL and threads

by CK-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi.

I'm writing dll for some program.
And i want to use TnFox in this dll.

I found topics:
   Using tnfox in a COM component
   Question about launching application itself in a separate thread

and tried to create Window in dll.
Ok. TnFox application works fine in dll.

But program's API is not threadsafe. And I must call API functions from
the dll thread.

In dll many threads with calculations and some GUI windows.

Worker thread must call API functions (but it can't).
I decided to send events/messages to main dll thread so API can be
safely called.

How I can attached to dll thread?
Can i receive in main (dll) thread event form worker thread?

I use MFC, TnFOX-0.88-FOXv1.6.33, MCVC++ 2005.

PS Sorry for english)



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Tnfox-discussion mailing list
Tnfox-discussion@...
https://lists.sourceforge.net/lists/listinfo/tnfox-discussion

Re: TnFox application in DLL and threads

by Niall Douglas :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 12 Sep 2008 at 0:15, Ck wrote:

> But program's API is not threadsafe. And I must call API functions from
> the dll thread.
>
> In dll many threads with calculations and some GUI windows.
>
> Worker thread must call API functions (but it can't).
> I decided to send events/messages to main dll thread so API can be
> safely called.
>
> How I can attached to dll thread?
> Can i receive in main (dll) thread event form worker thread?
>
> I use MFC, TnFOX-0.88-FOXv1.6.33, MCVC++ 2005.
>
> PS Sorry for english)

TnFOX copies the Windows model, so each thread has its own window
stack which only it can access. Multiple threads can have multiple
window stacks.

As you correctly point out, if a worker thread wishes to access a
window stack belonging to a different thread, it must post a message
to that thread using TnFXApp::postAsyncMessage() or more conveniently
wndh->getEventLoop()->postAsyncMessage().

If your main thread is a MFC thread, then you'll need to pump the FOX
message dispatch system manually from within the MFC message loop.

Does this help? Your email was a bit unclear.

Cheers,
Niall




-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Tnfox-discussion mailing list
Tnfox-discussion@...
https://lists.sourceforge.net/lists/listinfo/tnfox-discussion

Parent Message unknown Re: TnFox application in DLL and threads

by Niall Douglas :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 15 Sep 2008 at 15:44, Ck wrote:

> > If your main thread is a MFC thread, then you'll need to pump the FOX
> > message dispatch system manually from within the MFC message loop.
> >  
> How to do this?
> Can you give an example?

Look at the source for TnFXApp::run() and FXApp::run(). Basically it
runs via getNextEvent() and dispatchEvent().

> I think I need to use FXObject...
>
> struct Data;
> class Receiver: public FXObject
> {
> public:
>     ...
>     /** For sync calls. */
>     void syncCall(Data *data);
>
>     /** For async calls. */
>     long  tryHandle (FXObject *sender, FXSelector sel, void *ptr);
> }
>
> For sync call from worker thread I can do:
>     m_receiver->syncCall(m_data);
>
> For async:
>     m_receiver->getEventLoop()->postAsyncMessage(bla bla);
>
> How to pump Receiver message system from MFC message lopp?

See above.

If you wish to invoke some synchronous call which isn't threadsafe,
you should set up a message handler which receives async messages
from other threads and executes the required call. I personally would
have it pass a BoundFunctor such that any thread can invoke any
function in any other thread arbitrarily. I have an implementation of
this actually in Tn but not TnFOX - it wouldn't be hard to port over
though. I might add that to the todo list actually.

> PS Is this ok, what I write to you personally instead of to maillist?

I'd prefer the mailing list so others can search it in the future.

HTH,
Niall




-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Tnfox-discussion mailing list
Tnfox-discussion@...
https://lists.sourceforge.net/lists/listinfo/tnfox-discussion