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

Re: c++ event sink by Eric

Eric
Sat Apr 12 17:23:26 CDT 2008

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
>

Re: c++ event sink by Eric

Eric
Sat Apr 12 18:03:31 CDT 2008

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 ?

Re: c++ event sink by Anders

Anders
Sat Apr 12 19:55:15 CDT 2008

On Sat, 12 Apr 2008 14:44:53 -0700, Eric Kaplan
<tobycraftse@yahoo.com> wrote:

> 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

> Q: What's the most annoying thing on USENET?

Re: c++ event sink by Carmen

Carmen
Sat Apr 12 21:00:57 CDT 2008

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 ;
}






> 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.

Re: c++ event sink by Scott

Scott
Sat Apr 12 22:36:52 CDT 2008

"Carmen Sei" <fatwallet951@yahoo.com> wrote in message
news:93q2041troq411hmvcgd57pbv3qtsno891@4ax.com...
> 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]


Re: c++ event sink by Eric

Eric
Sun Apr 13 03:16:12 CDT 2008

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

Re: c++ event sink by Eric

Eric
Sun Apr 13 03:34:09 CDT 2008

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


>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);

Re: c++ event sink by Eric

Eric
Sun Apr 13 03:38:06 CDT 2008

I think make use of a call back function is good idea in here ??



>
>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);

Re: c++ event sink by Scott

Scott
Sun Apr 13 09:00:06 CDT 2008

"Eric Kaplan" <tobycraftse@yahoo.com> wrote in message
news:c1h304ln7p3pve7gqia6gurkqj8hr4m1k3@4ax.com...
> 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]


Re: c++ event sink by Eric

Eric
Sun Apr 13 17:48:40 CDT 2008

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


>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.

Re: c++ event sink by Scott

Scott
Sun Apr 13 21:22:08 CDT 2008

"Eric Kaplan" <tobycraftse@yahoo.com> wrote in message
news:uc3504ha15vvgce9f1elqqu0tpu67e4uma@4ax.com...
> is the PostMessage function a library call?
>
> which .h (header) file include this function?

#include <windows.h>

--
Scott McPhillips [VC++ MVP]


Re: c++ event sink by Ben

Ben
Wed Apr 16 11:03:44 CDT 2008

> 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.

> 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.