• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

NTservice is running but not calling my Worker thread

Here is the run method of my service,

void CTest::Run(DWORD dwArgc, LPTSTR * ppszArgv) {

     // report to the SCM that we're about to start
     ReportStatus(SERVICE_START_PENDING);

     m_hStop = ::CreateEvent(0, TRUE, FALSE, 0);
     m_hPause = ::CreateEvent(0, TRUE, FALSE, 0);
     m_hContinue = ::CreateEvent(0, TRUE, FALSE, 0);

     ReportStatus(SERVICE_RUNNING);
     
                       
     while( ::WaitForSingleObject(m_hStop, 10) != WAIT_OBJECT_0 )
     {
          // if the service got a "pause" request, wait until the user
          // wants to "continue".
          if(::WaitForSingleObject(m_hPause, 5) == WAIT_OBJECT_0)
          {
               while(::WaitForSingleObject(m_hContinue, 50) != WAIT_OBJECT_0)
                    if(::WaitForSingleObject(m_hPause, 50) == WAIT_OBJECT_0)
                         goto Stop;
               ::ResetEvent(m_hPause);
               ::ResetEvent(m_hContinue);
          }
          AddToMessageLog(TEXT("Service working ....Before Thread"),EVENTLOG_INFORMATION_TYPE);
// above meesage is comming into event message log

// this below statement is not executed at all
 AfxBeginThread(MyFunc,NULL);

         
     }
         
Stop:


     if( m_hStop )
          ::CloseHandle(m_hStop);
     if(m_hPause)
          ::CloseHandle(m_hPause);
     if(m_hContinue)
          ::CloseHandle(m_hContinue);
}




// this below statement is not executed at all
 AfxBeginThread(MyFunc,NULL);

why???
0
srithi
Asked:
srithi
  • 5
  • 5
  • 2
  • +1
1 Solution
 
jkrCommented:
Hmm, have you tried

AddToMessageLog(TEXT("Service working ....Before Thread"),EVENTLOG_INFORMATION_TYPE);
// above meesage is comming into event message log

// this below statement is not executed at all
AfxBeginThread(MyFunc,NULL);

AddToMessageLog(TEXT("Service working ....AFTER Thread"),EVENTLOG_INFORMATION_TYPE);

?

And, what is 'MyFunc()' like?
0
 
DanRollinsCommented:
How do you know that that line is not working?

You might be under the misconception that execution control will transfer to

  MyFunc

and keep running there for a while.  But instead, the AfxBeginThread() fn is called aand returns immediately.

-- Dan
0
 
srithiAuthor Commented:
i have increased the points by 50.


jkr,

// this below statement is not executed at all
AfxBeginThread(MyFunc,NULL);

>>AddToMessageLog(TEXT("Service working ....AFTER >>Thread"),EVENTLOG_INFORMATION_TYPE);

>>?
yeah i have put this ,but no message is comming into log.

Myfunc is a simple thread proc ,
UINT MyFunc(LPVOID lp)
{
// I WANT TO DO SOME FILE TRANSFERS FROM HERE

          return 1;
}

dan rollins,
as i said above its not calling the add message log funtion which i have put after AfxBeginThread,
as you said if the service just calls AfxBeginThread and returns,will it start the worker thread or not,can u give me some test code by which i can see whether Myfunc is executed or not.


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DanRollinsCommented:
You have a lot of unneeded complications if all you want to do is get some test code running... All of that Creating events and Waiting for events seems unnessicarity complicated for a first-draft.

>>can u give me some test code by which i can see whether Myfunc is executed or not.

I would just sprinkle some of these around:

    AfxMessageBox( "About to do Xxxxxxx" );
        ...
    AfxMessageBox( "Done with Xxxxxxx" );

    AfxMessageBox( "In the thread Proc" );
        ...
    AfxMessageBox( "Thread is exiting" );

=-=-=-=-=-=-=-
Also check your program flow... Remember that if the program exits before the thread gets going, the code in the thread will never execute.

-- Dan
0
 
srithiAuthor Commented:
can we have messageboxes in a service???,i tried to put some in mine but no message pops up.



>>Also check your program flow... Remember that if the >>program exits before the thread gets going, the
>>code in the thread will never execute.

my program does not terminate until i give a stop command
but still the thread does not work,
and istead of starting thread, i tried with a simple code which opens a file and writes something,but even this code is not executed.

thankx.



0
 
senohpCommented:
Place a MessageBeep(-1) in the MyFunc; so you can hear any beep in the PC speaker when the function run.
You service will produce beep at 10 ms interval.

UINT MyFunc(LPVOID lp)
{
  MessageBeep (-1) ;
  return 0 ;
}

I guest the fault is in AddToMessageLog. The function generate an exception that is not handled and terminate the whole process.
If the AddToMessageLog is OK, your program will add many lines to the event log.  

Regards

Seno Hardijanto
0
 
DanRollinsCommented:
Message box should work if you set the Service to "Allow service to interact with the desktop"  It is in the "Log On" tab of the Properties for the service, which is in Services, which is in the Windows Control Panel.

-- Dan
0
 
srithiAuthor Commented:
points increased by 50.

but i tried something like this
i created the thread as suspended and next called REsume thread ,and it worked,now from the loop its calling the thread ,but now the trouble is ,i have placed some code to open a text file and write something and close using CStdioFile object ,but this code is not getting executed,after some i am getting one assertion,i am unable to fix this,plz help me out.
thank you all.
0
 
jkrCommented:
>>can we have messageboxes in a service???,

Yes - all you need is to set MB_SERVICE_NOTIFICATION. Alternatively, use 'OutputDebugString()' and a viewer like e.g. http://www.sysinternals.com/ntw2k/freeware/debugview.shtml ("DebugView").
0
 
DanRollinsCommented:
Make sure that the code that you execute works in a normal situation.  That is, write a simple dialog-based or console-style application and test your code thoroughly in that environment.  Once you are certain that the code works, copy it into the Service.

Thanks for taking the time to read my humble suggestions.

-- Dan
0
 
srithiAuthor Commented:
dan Rollins,
thankx for the help,
can you suggest me ,a good book on NTServices ,
i will accept your next comment as answer.

thank you to all experts.
0
 
DanRollinsCommented:
sorry, I'm not big on book larn'in

I learned Services through the comprehensive information in MDSN and by using the AppWizard to create a Service, and then examining the code that it generated.

-- Dan
0
 
srithiAuthor Commented:
thankx dan.and thakx to all experts.

if u find some book plz pass the info to me.
my id is seshamsri@yahoo.com.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now