From: Phil on
Hello experts,
I am using MS VC 6.0 and Vista.
I am developing a dialog-based application. This application create a
thread using AfxBeginThread.
I need to be able to exit the new thread when clicking a button.
/////////////////////////////
MyAppDlg::OnStart()
{ // The user clicks a button to start a new thread
...
// the list of param I pass to the controlling function
static unsigned long ThreadParam[3];
// CEvent StopEvent is declared in MyAppDlg.h as "protected"
ResetEvent(StopEvent);
ThreadParam[0]=(unsigned long)&StopEvent;
// Begin the thread
AfxBeginThread((AFX_THREADPROC)MyControllingFunction,
(LPVOID)ThreadParam,THREAD_PRIORITY_NORMAL,0,0,NULL);
...
}
/////////////////////////////
MyAppDlg::OnStop()
{ // The user clicks a button to exit the new thread
SetEvent(StopEvent);
}
/////////////////////////////

I pass to the controlling function a pointer to the CEvent instance,
that I use to exit the thread.
Notice that the controlling function is global, not a static member of
MyAppDlg class.
/////////////////////////////
UINT MyControllingFunction(LPVOID ThreadParam)
{
HANDLE *pStopEvent=(HANDLE*)ThreadParam[0];
do
{
...
} while (WaitForSingleObject(*pStopEvent,100) != WAIT_OBJECT_0);
....
}
/////////////////////////////
This works great, except when I run multiple instances of my
application on the same
computer. When I click the "Stop" button of one app to exit its
thread, the other threads created by the
other applications stop too! I would not expect the CEvent object
being shared by the apps!
What did I do wrong?
I also declared the controlling function as a static member of
MyAppDlg, with the same result :(
From: David Lowndes on
>This works great, except when I run multiple instances of my
>application on the same
>computer. When I click the "Stop" button of one app to exit its
>thread, the other threads created by the
>other applications stop too! I would not expect the CEvent object
>being shared by the apps!
>What did I do wrong?

Have you prefixed the name of the event with "Global\" by any chance?

Dave
From: Phil on
On 6 mai, 13:21, David Lowndes <Dav...(a)example.invalid> wrote:
> >This works great, except when I run multiple instances of my
> >application on the same
> >computer. When I click the "Stop" button of one app to exit its
> >thread, the other threads created by the
> >other applications stop too! I would not expect the CEvent object
> >being shared by the apps!
> >What did I do wrong?
>
> Have you prefixed the name of the event with "Global\" by any chance?
>
> Dave

Nope
From: ChrisN on
On Tue, 6 May 2008 02:01:31 -0700 (PDT), Phil <pbruyant(a)yahoo.com>
wrote:

>This works great, except when I run multiple instances of my
>application on the same
>computer. When I click the "Stop" button of one app to exit its
>thread, the other threads created by the
>other applications stop too! I would not expect the CEvent object
>being shared by the apps!
>What did I do wrong?
>I also declared the controlling function as a static member of
>MyAppDlg, with the same result :(

You'll get the behaviour you describe if you specified a name for the
event object when it was constructed:

CEvent Event( FALSE, FALSE, _T( "MyEvent" ), NULL );

This creates an event that is shared between processes.

To ensure that the event is unique to each process, specify NULL for
the lpszName parameter:

CEvent Event( FALSE, FALSE, NULL, NULL );

Chris
From: Phil on
On 6 mai, 13:39, ChrisN <chrisn...(a)nospam.googlemail.com> wrote:
> On Tue, 6 May 2008 02:01:31 -0700 (PDT), Phil <pbruy...(a)yahoo.com>
> wrote:
>
> >This works great, except when I run multiple instances of my
> >application on the same
> >computer. When I click the "Stop" button of one app to exit its
> >thread, the other threads created by the
> >other applications stop too! I would not expect the CEvent object
> >being shared by the apps!
> >What did I do wrong?
> >I also declared the controlling function as a static member of
> >MyAppDlg, with the same result :(
>
> You'll get the behaviour you describe if you specified a name for the
> event object when it was constructed:
>
> CEvent Event( FALSE, FALSE, _T( "MyEvent" ), NULL );
>
> This creates an event that is shared between processes.
>
> To ensure that the event is unique to each process, specify NULL for
> the lpszName parameter:
>
> CEvent Event( FALSE, FALSE, NULL, NULL );
>
> Chris

Thank you so much Chris !
Indeed, I had named my events. Specifying NULL for the names solved
the problem.
Thanks again !
Phil