CreateEvent!!!

I have created an NT Services that pulls files from a Directory and transports over to a MSMQ. The Service constantly runs. It waits for single Object in the directory as soon as it sees a new file it pulls it away to the MSMQ. The problem is whenever I try to stop the service it returns me error(thats quite normal coz its constantly running). I am trying to Create an Event that can shutdown the service. Can anyone give me some feedback how to accomplish this task. I got a break from my colleague as he said me to implement waitformultiple objects instead for wait for single object.

Thx in adv.
veselAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
To use an event that terminates a service, use the following code
SERVICE_STATUS          g_ssStatus;       // current status of the service

void StopService()
{
    SC_HANDLE   schService;
    SC_HANDLE   schSCManager;

    schSCManager    =   OpenSCManager   (   NULL,                   // machine (NULL == local)
                                            NULL,                   // database (NULL == default)
                                            SC_MANAGER_ALL_ACCESS   // access required
                                        );
    if  (   schSCManager)
        {
            schService  =   OpenService (   schSCManager,
                                            TEXT    (   _SERVICE_NAME),
                                            SERVICE_ALL_ACCESS
                                        );

            if  (   schService)
                {
                    // try to stop the service
                    if  (   ControlService  (   schService,
                                                SERVICE_CONTROL_STOP,
                                                &g_ssStatus
                                            )
                        )
                        {
                            Sleep   (   1000 );

                            while   (   QueryServiceStatus  (   schService,
                                                                &g_ssStatus
                                                            )
                                    )
                                    {
                                        if  (       SERVICE_STOP_PENDING    
                                                ==  g_ssStatus.dwCurrentState
                                            )
                                            {
                                                Sleep( 1000 );
                                            }
                                        else
                                                break;
                                    }

                            if  (   SERVICE_STOPPED ==  g_ssStatus.dwCurrentState)
                                {
                                    //  success
                                }
                            else
                                {
                                    //  error
                                }

                        }

                CloseServiceHandle  (   schService);
            }
            else
            {
                //  error
            }


            CloseServiceHandle  (   schSCManager);
        }
    else
        {
            //  error
        }
}

and call this code from a thread that waits for the event:

ULONG   __stdcall   TerminatorThread    (   LPVOID);

#define OBJECT_NAME "TerminatorEvent"

void    main    (   void)
{
    HANDLE  hEvent;
    HANDLE  hThread;
    DWORD   dwTID;

    hEvent  =   CreateEvent (   NULL,   FALSE,  FALSE,  OBJECT_NAME);

    if  (   ERROR_ALREADY_EXISTS    ==  GetLastError    ())
        {
            printf  (   "main():\tsetting event...\n");

            SetEvent    (   hEvent);
        }

    hThread =   CreateThread    (   NULL,
                                    0,
                                    TerminatorThread,
                                    ( LPVOID) hEvent,
                                    0,
                                    &dwTID
                                );

    // ...

    CloseHandle (   hEvent);
    CloseHandle (   hThread);
}

ULONG   __stdcall   TerminatorThread    (   LPVOID  pv)
{
    HANDLE  hEvent  =   ( HANDLE)   pv;

    WaitForSingleObject (   hEvent, INFINITE);

    StopService ();

    return  (   0);
}

Feel free to ask if you need more information!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
veselAuthor Commented:
I did it finally but hey thx again
0
jkrCommented:
Fine that it works for you!

BTW: Just out of curiosity - your handle sounds like a turkish name, am I right?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.