Threading and callback problem

  • Thread starter Thread starter Sylvain Ross
  • Start date Start date
S

Sylvain Ross

Hi,

I'm trying to do this :

1) Start a new thread called "FillingThread" from my main thread
2) when my FillingThread is finnished, I wan't the main thread to be
notified.

But I don't see anyway to call a function in the main thread from the
"FillingThread" thread.

If any one got an idea ...
I'm just trying to be notified in the main thread that the
"fillingThread" is finnsihed without any hack such as a loop while
thread not finished...

Note that I DO wan't to use Thread because I need to be able to cancel it.

Thx in advance !
 
there are three ways:

1) saving the handle returned by CreateThread() and use
WaitForSingleObject/WaitForMultipleObjects on it.
2) create an event with CreateEvent() and share such a HANDLE with the new
thread, then the thread is done, before returning, it will SetEvent() such
a handle. while the WinMain() is doing a
WaitForSingleObject()/WaitForMultipleObjects()....
3) You create a global flag: static BOOL bThreadExited = FALSE; you call the
createthread() and while the thread is running.... you do your own thing in
the main(), while you check the flag here and there perhaps in a polling
thread until == TRUE.
 
Thx Andrea,

Well the problem is that all your solutions seems to "block" my main thread.

When I run WaitForSingleObject(), the main thread is blocked on this
statement, then my user interface is completely frozen, what I dont want.

Same thing with a loop until process is alive, I won't be able to use my UI.


The idea should be to create an event which will be fired from my
working thread, and caught into my main thread... but I've been looking
and trying a lot of thing this afternoon and finally nothing works ...

I can't beleive that a such thing is undoable, the sdk timer works like
this, it runs in another thread, but fire an event in the main thread...
So it must be possible !
 
trust me it's all doable,
I just need to get more into details of what Ecactly you need to do.
I understand now the main() needs to also do other things instead of
blocking.

From what I understand so far, the main() needs to be operating in the
meanwhile instead of waiting.
it really depends on what you need to be doing in the main() though, if you
are doing some other stuff such as updating files, UI and so on...
there must be a while/loop that does so, in which you routinely check a
given global flag without even the need of an event.

if you must be able to update UI and do other OS things and in any give
time, even during those things are still working, the worker thread needs to
be
doing things, well, such a thread will have to spawn it's own
multitasked/overlapped code.

just remember that, you can not / not supposed to INTERRUPT a major OS
call...

example:
if the main() you are doing long term operations such as file creations and
another thread wants to 'somehow' interrupt those main()'s operations that
are still operating...
the result could be catastrophic.
interrupting the an OS function in the middle of the flow, generally loses
permanently 1MB of stack, doesnt free rerources and corrupts data.

the approaches are after all some of these:
1) you loop in the main until a flag is changed, and in the loop you do your
updates and operations.
2) you WaitForSingle/MultipleObjects doing nothing until an event in fired
by a worker thread.
3) if you dont want a loop/flag, and yet you have to do things, you can
always have the main() do certain update operations... and let it take how
ever long it takes...
prespawn the worker thread so it also operates in an overlapped fashion,
after the main()'s code is done with its deal, instead of the loop, put a
WaitForSingleObject on the worker thread's handle.
this way the main may take any time needed, however long, and
'synchronize' with the worker thread whenever it ends
 
Back
Top