Discussion:
c++ event sink
(too old to reply)
Eric Kaplan
2008-04-12 21:44:53 UTC
Permalink
I have a function that will download XML from internet and load XML
data into database.

The function will take 5 - 20 minutes to finish.

I heard I should use event sink (event listener) when function is
finished the task, then it will notify the caller.

So I am planning to create a seperate thread to do the long XML
loading function by using - _beginthreadex()

But how to create a notification / event sink / event listener in C++?

any library can easily just a library function call?

or any sample source code on the internet?

Thanks
Eric Kaplan
2008-04-12 22:23:26 UTC
Permalink
how about if my library / DLL dont' use MFC at all,

but may be my friend's appliction that make use of my library / DLL
use MFC i don't know, in this case, can I use _beginthreadex() ???

also where is your essay on worker thread??

Thanks again
See my essay on worker threads.
Note: you must *not* use _beginthread in MFC; you *must* use AfxBeginThread.
I would pass in a CWnd* in some form or other, and PostMessage to it when the worker
thread is finished.
joe
Eric Kaplan
2008-04-13 08:16:12 UTC
Permalink
what if my DLL library "DO NOT" use any MFC, but my friend's code
callin my DLL is using MFC?

can I still use _beginthread / _beginthreadexe???
If you do not use MFC, you use _beginthread/_beginthreadex. In such cases I would pass
the HWND of the window and call ::PostMessage to send notifications.
You can find the essay on my MVP Tips site
www.flounder.com/mvp_tips.htm
Eric Kaplan
2008-04-12 23:03:31 UTC
Permalink
If I want to start another process instead of a new thread, how can I
do that?

since the loading of XML into database takes at least 5min to 30 min.

how to I notify the caller if i start another process instead of
another thread ?
Anders Karlsson
2008-04-13 00:56:58 UTC
Permalink
On Sat, 12 Apr 2008 14:44:53 -0700, Eric Kaplan
Post by Eric Kaplan
I have a function that will download XML from internet and load XML
data into database.
The function will take 5 - 20 minutes to finish.
I heard I should use event sink (event listener) when function is
finished the task, then it will notify the caller.
So I am planning to create a seperate thread to do the long XML
loading function by using - _beginthreadex()
But how to create a notification / event sink / event listener in C++?
any library can easily just a library function call?
or any sample source code on the internet?
Thanks
If you want to download the XML-file in a separate thread just
create a function that downloads the file and then launch it in its
own thread passing a pointer to the parent object/window - depending
in which environment you are running.

When the download is complete, you post a message to the parent
object/window saying that you are done. You can also continously post
messages to the parent to display the progress of the copy - probably
a good idea in your case since you say it takes so long to complete.

hth/Anders.
--
A: People bitching about top-posting
Post by Eric Kaplan
Q: What's the most annoying thing on USENET?
Carmen Sei
2008-04-13 02:00:57 UTC
Permalink
are you meaning I should create a new thread from the primary thread
currently running?

then I pass a pointer of status to that thread like -

==============================
int * status;

// a new thread get initialized and pass a pointer of the status
// to the new thread
ThreadX * o1 = new ThreadX( &status );

// create the thread using _beginthreadex()
hth1 = (HANDLE)_beginthreadex(
NULL, // security
0, // stack size
ThreadX::ThreadStaticEntryPoint,
o1, // arg list
CREATE_SUSPENDED, // so we can later call ResumeThread()
&uiThread1ID
);

// after created the thread, primary thread continue execute
// primary thread keeps checking the status
// (integer value 1 = finish, integer value 0 = not finish)


==============================

ThreadX::ThreadX( int * status )
{
// if the XML thread finish loading data
// may be after 10 min, the status flag is set to 1 = finished
currentstatus = status ;
}
Post by Anders Karlsson
If you want to download the XML-file in a separate thread just
create a function that downloads the file and then launch it in its
own thread passing a pointer to the parent object/window - depending
in which environment you are running.
When the download is complete, you post a message to the parent
object/window saying that you are done. You can also continously post
messages to the parent to display the progress of the copy - probably
a good idea in your case since you say it takes so long to complete.
hth/Anders.
Scott McPhillips [MVP]
2008-04-13 03:36:52 UTC
Permalink
Post by Carmen Sei
are you meaning I should create a new thread from the primary thread
currently running?
then I pass a pointer of status to that thread like -
==============================
int * status;
// a new thread get initialized and pass a pointer of the status
// to the new thread
ThreadX * o1 = new ThreadX( &status );
// create the thread using _beginthreadex()
hth1 = (HANDLE)_beginthreadex(
NULL, // security
0, // stack size
ThreadX::ThreadStaticEntryPoint,
o1, // arg list
CREATE_SUSPENDED, // so we can later call ResumeThread()
&uiThread1ID
);
// after created the thread, primary thread continue execute
// primary thread keeps checking the status
// (integer value 1 = finish, integer value 0 = not finish)
==============================
ThreadX::ThreadX( int * status )
{
// if the XML thread finish loading data
// may be after 10 min, the status flag is set to 1 = finished
currentstatus = status ;
}
Is your program a console program, or a windowed program? If it is a
windowed program then it must contain a message pump. So the way to signal
the main thread is to pass it a message. First pass an HWND to the
secondary thread. When the secondary thread is ready to signal the main
thread it should do so with PostMessage to the HWND. Use a custom message
you define like this:

#define UWM_THREAD_DONE (WM_APP + 1)

PostMessage(hwnd, UWM_THREAD_DONE, anyparam, anyparam);
--
Scott McPhillips [VC++ MVP]
Eric Kaplan
2008-04-13 08:34:09 UTC
Permalink
It's a DLL library.

It will be used by an OpenGL graphics program.

which uses the openframeworks
http://www.openframeworks.cc/documentation

Is it a console application then?? ( it can run in full screen or
window mode though)

for #define UWM_THREAD_DONE (WM_APP + 1)

are you meaning define some kind of "CALL BACK" macros?

once the 2nd thread is done, then it updates the macro?

that's the idea of "CALL BACK" like here -
http://support.microsoft.com/kb/124103
Post by Scott McPhillips [MVP]
Is your program a console program, or a windowed program? If it is a
windowed program then it must contain a message pump. So the way to signal
the main thread is to pass it a message. First pass an HWND to the
secondary thread. When the secondary thread is ready to signal the main
thread it should do so with PostMessage to the HWND. Use a custom message
#define UWM_THREAD_DONE (WM_APP + 1)
PostMessage(hwnd, UWM_THREAD_DONE, anyparam, anyparam);
Scott McPhillips [MVP]
2008-04-13 14:00:06 UTC
Permalink
Post by Eric Kaplan
It's a DLL library.
It will be used by an OpenGL graphics program.
which uses the openframeworks
http://www.openframeworks.cc/documentation
Is it a console application then?? ( it can run in full screen or
window mode though)
for #define UWM_THREAD_DONE (WM_APP + 1)
are you meaning define some kind of "CALL BACK" macros?
once the 2nd thread is done, then it updates the macro?
that's the idea of "CALL BACK" like here -
http://support.microsoft.com/kb/124103
A graphics program is not a console program. A graphics program displays
windows and must continually process window messsages. That is why you
should use PostMessage to communicate events to the main thread. CALLBACKS
are not relevant for interthread communication: You cannot "call" another
thread.
--
Scott McPhillips [VC++ MVP]
Eric Kaplan
2008-04-13 22:48:40 UTC
Permalink
is the PostMessage function a library call?

which .h (header) file include this function?

like
http://forums.fanatic.net.nz/lofiversion/index.php/t9368.html
Post by Scott McPhillips [MVP]
A graphics program is not a console program. A graphics program displays
windows and must continually process window messsages. That is why you
should use PostMessage to communicate events to the main thread. CALLBACKS
are not relevant for interthread communication: You cannot "call" another
thread.
Scott McPhillips [MVP]
2008-04-14 02:22:08 UTC
Permalink
Post by Eric Kaplan
is the PostMessage function a library call?
which .h (header) file include this function?
#include <windows.h>
--
Scott McPhillips [VC++ MVP]
Ben Voigt [C++ MVP]
2008-04-16 16:03:44 UTC
Permalink
Post by Scott McPhillips [MVP]
A graphics program is not a console program. A graphics program
displays windows and must continually process window messsages. That
A console program is linked for the console subsystem, it is given a console
at startup and will use the console of the parent if there was one.
A non-console program is linked for the windows subsystem, it is not given a
console at startup.
The non-console can create a console, but I do not believe it can share with
the parent process.
Either console and non-console program may create windows and display
graphics, neither one needs to. Services, for example, often have neither a
console nor a window, and certainly no visible window.
Any thread that creates even one window, even if inside a console program,
must dispatch messages frequently.
Post by Scott McPhillips [MVP]
is why you should use PostMessage to communicate events to the main
thread. CALLBACKS are not relevant for interthread communication: You
cannot "call" another thread.
Eric Kaplan
2008-04-13 08:38:06 UTC
Permalink
I think make use of a call back function is good idea in here ??
Post by Scott McPhillips [MVP]
Is your program a console program, or a windowed program? If it is a
windowed program then it must contain a message pump. So the way to signal
the main thread is to pass it a message. First pass an HWND to the
secondary thread. When the secondary thread is ready to signal the main
thread it should do so with PostMessage to the HWND. Use a custom message
#define UWM_THREAD_DONE (WM_APP + 1)
PostMessage(hwnd, UWM_THREAD_DONE, anyparam, anyparam);
Continue reading on narkive:
Loading...